frm_new_post

Back to Top
Knowledge BaseEntry Management → frm_new_post

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

Usage

add_filter('frm_new_post', 'modify_my_post', 10, 2);

Parameters

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

Examples

Formidable Hook - Helpful

Change the post parent

Submitted by  — 8 years ago

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;
}
Formidable Hook - Helpful

Use two fields for the title

Submitted by  — 8 years ago

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;
}
Formidable Hook - Helpful

Create products in EDD

Submitted by  — 8 years ago

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;
}
Formidable Hook - Helpful

Create a custom field

Submitted by  — 8 years ago

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'] = wp_get_attachment_url( $field_value ); // uncomment to save the attachment url and change 'custom_field_name_here'
    //$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;
}
If you would like to add your own example, click here.

Have something to add?

Click here to provide feedback on this page.