Formidable Forms

Formidable Forms

  • Features
  • Pricing
  • Resources
    • Docs & Support
    • Blog
    • Community
  • Solutions
    • Web Applications
    • Calculators
    • Surveys
    • Directories
    • Payments
    • Contact forms
  • Login
  • Get Formidable Forms

frm_new_post

Last Updated: June 4, 2018

Knowledge Base → Extend Formidable Forms → Formidable Hooks - for Developers → Entry Management → frm_new_post
Heads up!
This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

This hook is used to modify the post before it is created.

Formidable Forms is the best WordPress Form Builder plugin. Get it for free!

Usage

add_filter('frm_new_post', 'modify_my_post', 10, 2);
function modify_my_post($post, $args)

Parameters

  • $post (array)
  • $args['form'] (object)
  • $args['entry'] (object)
  • $args['action'] (object)

Examples

Change the post parent

If you would like your created posts to have a parent, you can set it with this code.

add_filter( 'frm_new_post', 'change_my_post_parent', 10, 2 );
function change_my_post_parent( $post, $args ) {
        if ( $args['form']->id == 25 ) { //change 25 to the ID of your form
                $post['post_parent'] = 30; //change 30 to the ID of your WP parent page
	}
	return $post;
}

Use two fields for the title

When you customize the post title, it's best to not select a field to set it. This function will set the title instead of using the settings.

Note: The fields you combine will not be automatically updated if your post field is changed from the WordPress posts page.

add_filter( 'frm_new_post', 'change_my_post_title', 10, 2 );
function change_my_post_title( $post, $args ) {
    if ( $args['form']->id == 25 ) { //change 25 to the ID of your form
        $title = $_POST['item_meta'][20] .' '. $_POST['item_meta'][21] .' '. $_POST['item_meta'][22] .' '. $_POST['item_meta'][23];
        //change each number (20, 21, 22, 23) to the ID of the field to insert
        $post['post_title'] = $title;
    }
    return $post;
}

Create products in EDD

Use your forms to create downloads in Easy Digital Downloads.

add_filter('frm_new_post', 'edd_setup_files', 10, 2);
function edd_setup_files($post, $args) {
        if ( $args['form']->id != 5 ) { //change 5 to the ID of your form
		return $post;
	}

	global $frm_vars;
	// don't continue if no files were uploaded
	if( ! isset( $frm_vars['media_id'] ) || empty( $frm_vars['media_id'] ) ) {
		return $post;
	}

	$edd_files = array();
	foreach ( (array) $frm_vars['media_id'] as $media_id ) {
		foreach ( (array) $media_id as $id ) {
			$attachment = get_post( $media_id );
			$edd_files[] = array(
				'file'      => wp_get_attachment_url( $id ),
				'condition' => '',

				// change this line to get the name you want
				'name'      => basename( $attachment->guid ),
			);
		}
	}

	$post['post_custom']['edd_download_files'] = $edd_files;
	return $post;
}

Save custom date format in custom field

You may have the need to create a custom field with the data format saved differently than Formidable saves by default. This is a common with saving date fields in a different format and saving the image url for a file upload field instead of the id. When using this custom code the custom field name should not be included in your post settings for this form.

add_filter( 'frm_new_post', 'create_a_custom_field', 10, 2 );
function create_a_custom_field( $post, $args ) {
  $field_id = 25; // change 25 to the id of the field to draw from
  if ( isset( $_POST['item_meta'][ $field_id ] ) ) {
    $field_value = sanitize_text_field( $_POST['item_meta'][ $field_id ] );
    //$post['post_custom']['custom_field_name_here'] = date( 'm-d-Y', $field_value ); // uncomment to save a different date format and change 'custom_field_name_here'
  }
  return $post;
}

Store uploaded file URL in custom field

Use the following code to store an uploaded file URL in a custom field. When using this custom code the custom field name should not be included in your post settings for this form.

add_filter( 'frm_new_post', 'create_a_custom_field', 10, 2 );
function create_a_custom_field( $post, $args ) {
  $field_id = 25; // change 25 to the id of the file upload field
  if ( isset( $_POST['item_meta'][ $field_id ] ) ) {
    $field_value = sanitize_text_field( $_POST['item_meta'][ $field_id ] );
    $post['post_custom']['custom_field_name_here'] = wp_get_attachment_url( $field_value ); // change 'custom_field_name_here' to your custom field name
  }
  return $post;
}

Save entry ID in custom field

Use the following code to insert the ID of a newly created entry in a custom field. The post must be created by a Formidable form. Replace 25 with the ID of your form. Replace my_custom_field with your custom field name.

add_filter( 'frm_new_post', 'frm_save_entry_id_to_custom_field', 10, 2 );
function frm_save_entry_id_to_custom_field( $post, $args ) {
	if ( $args['form']->id == 25 ) { //change 25 to the ID of your form
		$post['post_custom']['my_custom_field'] = $args['entry']->id;
	}
	return $post;
}

Set post status based on user role

Use the following code snippet to set the post status based on the user role. When using this, add a hidden field first in your form with a default value set to [user role].

add_filter( 'frm_new_post', 'set_post_status', 10, 2 );
function set_post_status( $post, $args ) {
    if ( $args['form']->id == 68 ) { //change 68 to the ID of your form
        $role = $_POST['item_meta'][41065];  //Change 41065 to the ID of the hidden field with default value set to [user_role]
        $status="private";
		if($role=='editor')
		{
		  $status="draft";
		}elseif($role=='administrator')
		{
			$status="publish";
		}
        $post['post_status'] = $status;
    }
    return $post;
}

Change Log

Added in version 2.0
  • Usage
  • Parameters
  • Examples
    • Change the post parent
    • Use two fields for the title
    • Create products in EDD
    • Save custom date format in custom field
    • Store uploaded file URL in custom field
    • Save entry ID in custom field
    • Set post status based on user role
  • Change Log
Categories
×

Categories

  • Installation & Getting Started
  • Account Management
  • Forms
  • Entries
  • Views
  • Styles
  • Importing & Exporting
  • Add-Ons
  • Extend Formidable Forms

Using WordPress and want to get Formidable Forms for free?

Get Formidable Forms Lite Now

You do not have permission to view this form. Maybe you need to log in?

This article may contain affiliate links. Once in a while, we earn commissions from those links. But we only recommend products we like, with or without commissions.

Take on bigger projects Right Now

Get the tools you need to revolutionize your workflow and architect a masterpiece. Build the most advanced WordPress forms and actually use the data you collect in meaningful ways.

Get the most advanced WordPress form plugin and the only form builder with integrated Views.

Get Formidable Forms Now

Resources

  • Community
  • Affiliates
  • Contact
  • Free Online Form Builder

Top Features

  • Application Builder
  • Calculator Forms
  • Surveys & Polls
  • Quiz Maker
  • Form Templates
  • Application Templates
  • Directories
  • Donation Plugin

Company

  • About Us
  • Giving Back
  • Careers
  • Newsletter
  • WP Tasty
  • Nutrifox

Copyright © 2023 Strategy11, LLC. Formidable FormsĀ® is a registered trademark Strategy11, LLC.
Privacy Policy | Terms of Service | Sitemap

Join 300,000+ using Formidable Forms to create form-focused solutions fast. Get Started See User Reviews