frm_after_create_entry

Back to Top
Knowledge BaseEntry Management → frm_after_create_entry

This hook allows you to do something with the data entered in a form after it is submitted.

Usage

add_action('frm_after_create_entry', 'after_entry_created', 30, 2);

Parameters

  • $entry_id (integer)
  • $form_id (integer)

Examples

Formidable Hook - Helpful

Move and rename files

Submitted by  — 5 years ago

Use values from your entries to organize and rename your file uploads. Before using this, we recommend you try a more simple option.

add_action( 'frm_after_create_entry', 'frm_move_file_and_rename' );
function frm_move_file_and_rename() {
	$field_ids = array( 25, 26 ); // change field IDs to the IDs of your file upload fields
	foreach ( $field_ids as $field_id ) {
		if ( isset( $_POST['item_meta'][ $field_id ] ) ) {
			if ( ! frm_filesystem_prepared() ) {
				return;
			}

			$file_ids = array_filter( (array) $_POST['item_meta'][ $field_id ], 'is_numeric' );
			
			foreach ( $file_ids as $file_id ) {
				$file_id = absint( $file_id );
				$old_source = get_attached_file( $file_id );
				$file_name = basename( $old_source );

				// change the filename here
				$file_name = sanitize_title( $_POST['item_meta'][ 41150 ] ) . '-' . $file_name; // Change 41150 to the id of your file name field. remove this line to leave the filename as is

				// set folder name here
				$new_folder_name = 'formidable/' . sanitize_title( $_POST['item_meta'][ 41335 ] ); // Change 41335 to the id of the folder name field

				$file_info = new FrmCreateFile( array( 'folder_name' => $new_folder_name, 'file_name' => $file_name ) );
				frm_create_directories( $file_info );

				$destination = $file_info->uploads['basedir'] . '/' . $new_folder_name . '/' . $file_name;

				global $wp_filesystem;
				if ( $wp_filesystem->move( $old_source, $destination ) ) {
					update_attached_file( $file_id, $destination );
				}
			}
		}
	}
}

function frm_filesystem_prepared() {
	if ( ! is_admin() || ! function_exists( 'get_filesystem_method' ) ) {
		include_once(ABSPATH . 'wp-admin/includes/file.php');
	}

	$access_type = get_filesystem_method();
	if ( $access_type === 'direct' ) {
		$creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() );
	}

	return ( ! empty( $creds ) && WP_Filesystem( $creds ) );
}

function frm_create_directories( $file_info ) {
	global $wp_filesystem;

	$needed_dirs = frm_get_needed_dirs( $file_info );
	foreach ( $needed_dirs as $_dir ) {
		// Only check to see if the Dir exists upon creation failure. Less I/O this way.
		if ( $wp_filesystem->mkdir( $_dir, $file_info->chmod_dir ) || $wp_filesystem->is_dir( $_dir ) ) {
			$index_path = $_dir . '/index.php';
			$wp_filesystem->put_contents( $index_path, "chmod_file );
		}
	}
}

function frm_get_needed_dirs( $file_info ) {
	$dir_names = explode( '/', $file_info->folder_name );
	$needed_dirs = array();

	$next_dir = '';
	foreach ( $dir_names as $dir ) {
		$next_dir .= '/' . $dir;
		$needed_dirs[] = $file_info->uploads['basedir'] . $next_dir;
	}

	return $needed_dirs;
}
Formidable Hook - Helpful

Change user role after entry submission

Submitted by  — 7 years ago

Use this code to change a user's role after he/she submits an entry in a specific form.

/**
* This will change an inactive user to a member after they complete their member profile.
*/
add_action('frm_after_create_entry', 'inactive_to_member', 20, 2);
function inactive_to_member($entry_id, $form_id){
 if($form_id == 24){ //change 24 to the form id of the form to copy
   $new_role = 'member'; //change this to the role users should be granted upon completing form

   $user = wp_get_current_user(); //get logged in user
   if(!$user) {
       return; //don't continue if user doesn't exist
   }

   $updated_user = (array)$user;

   // Get the highest/primary role for this user  
   $user_roles = $user->roles;
   $user_role = array_shift($user_roles);
   if ( $user_role == 'administrator' ) 
       return; //make sure we don't downgrade any admins

   $updated_user['role'] = $new_role;
    
   wp_update_user($updated_user);
 }
}
Formidable Hook - Helpful

Add image meta

Submitted by  — 7 years ago

Use this code to programmatically add an "alt" to files uploaded in a Formidable form. The "alt" will use the name of the uploaded file.

add_action('frm_after_create_entry', 'add_uploaded_file_alt', 30, 2);
function add_uploaded_file_alt( $entry_id, $form_id ) {
	if ( $form_id == 5 ) { //replace 5 with the id of the form
		// Get all uploaded file attachment IDs
		$media_ids = $_POST['item_meta'][519];//Replace 519 with the ID of your file upload field
		
		foreach ( (array)$media_ids as $id ) {
			if ( ! $id ) {
				continue;
			}

			// Get file name
			$label = basename( get_attached_file( $id ) );

			// Add alt
			update_post_meta( $id, '_wp_attachment_image_alt', $label );
		}
	}
}
Formidable Hook - Helpful

Copy the user_id from an embedded form to the main form

Submitted by  — 7 years ago

Use this code to copy the user_id from an embedded form to the main form. For example, you can insert your registration as an embedded form of your main form, have users register in it and use this to link your main form to the user.

$global_user_id;

add_action('frm_after_create_entry', 'after_entry_created', 80, 2);
function after_entry_created($entry_id, $form_id){
  global $global_user_id, $wpdb;

  $embedded_form_id = 9; // embedded form id
  $main_form_id = 5; // parent form id

  if ( $form_id == $embedded_form_id ) {
    // Get the entry of the embedded form
    $entry = FrmEntry::getOne( $entry_id );
    // Get the user_id from the entry
    $user_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->prefix}frm_items WHERE id=%d", $entry_id ));
    // Save it to a global variable
    $global_user_id= $user_id;
  } 
  else if ( $form_id == $main_form_id ) {
    // Get the entry of the main form
    $entry = FrmEntry::getOne( $entry_id );
    // Update the user_id of the main form with the one saved from the embedded form
    FrmRegEntry::update_user_id_for_entry( $form_id, $entry, $global_user_id);
  }
}
Formidable Hook - Helpful

Create an entry in another form

Submitted by  — 5 years ago

Use this code to create an entry in Form B when you submit an entry in Form A. A field in Form B will have the user ID from Form A. Another will have the entry ID of Form A entry. And a final will have the value of another field in Form A. If you use the code as written, the user ID and additional value are treated as required. If they're not present, the entry won't be created. You can remove or comment out code you don't want to use.

add_action('frm_after_create_entry', 'create_entry_after_submission', 30, 2);

function create_entry_after_submission($entry_id, $form_id){
    if ( $form_id != 372 ) {//Change 372 to the ID of Form A
        return;
    }

    $form2 = '373';//Change 373 to the ID of Form B
    //Get user  
    if (!isset($_POST['frm_user_id'])){
        return;
    }
    $user = $_POST['frm_user_id'];
    //get data
    if (!isset($_POST['item_meta'][6614])){ //replace 6614 with the field ID of the Form A value
        return;
    }
    $new_value = $_POST['item_meta'][6614];
    //create entry
    FrmEntry::create(array(
        'form_id' => $form2,
        'item_key' => 'entry', //change entry to a dynamic value if you would like
        'frm_user_id' => $user,
        'item_meta' => array(
             6620 => $entry_id, //change 6620 to the ID of the field you want to populate (in Form B) with the entry ID of Form A
             6621 => $user,//change 6621 to the ID of the userID field in Form B
             6622 => $new_value,//Change 6622 to the ID of a field in Form B you want to populate with a value from Form A
        ),
    ));
}

Change 372 for the id of Form A, 373 for the id in Form B, 6614 for the field ID which holds the value in Form A you want to add to Form B, 6620 for the field ID in Form B that stores the Form A entry ID, 6621 for the field ID of the User ID field in Form B, and 6622 for the field ID in Form B that has the value from form A.

Formidable Hook - Helpful

Save the entry ID in a field

Submitted by  — 5 years ago

This code allows you to save the entry ID of the current entry in one of its fields.

add_action('frm_after_create_entry', 'frm_add_entry_id', 42, 2);
function frm_add_entry_id($entry_id, $form_id){
  if ( $form_id == 221 ) { //change 221 to the ID of your form
	FrmEntryMeta::add_entry_meta( $entry_id, 1819, "",  $entry_id);//change 1819 to the ID of the field in which you want to store the entry ID
  }
}

Change 221 to the id of your current form and 1819 to the id of the field where you'd like to store the entry ID.

Notes:

  • For this code to work properly, the field that stores the entry ID must be empty. It shouldn't have a default value, and users shouldn't be able to enter a value into it.
  • This code should only be used with the frm_after_create_entry hook, not the frm_after_update_entry hook.
Formidable Hook - Helpful

Delete the entry, leave the post

Submitted by  — 7 years ago

When the "Do not store entries submitted from this form" box is checked, but you are also creating posts, neither the entry or the post are saved. If you'd like the entry deleted, but the post to remain, do not check that box, but use this instead.

add_action('frm_after_create_entry', 'after_entry_created', 60, 2);
function after_entry_created( $entry_id, $form_id ) {
  global $wpdb;
  // unlink the post from the entry
  $wpdb->update( $wpdb->prefix .'frm_items', array( 'post_id' => '' ), array( 'id' => $entry_id ) );

  // now the entry can be deleted
  FrmEntry::destroy( $entry_id );
}
Formidable Hook - Helpful

Automatically delete files

Submitted by  — 7 years ago

If you would like to delete uploaded files from your site immediately after they are submitted, use this example.

add_action('frm_after_create_entry', 'after_entry_created', 50, 2); //use 50 to make sure this is done very last
function after_entry_created($entry_id, $form_id){
  if($form_id == 5){ //change 5 to the ID of your form
    $field_id = 25; //change 25 to the ID of the upload field
    if(isset($_POST['item_meta'][$field_id])){
      if(is_array($_POST['item_meta'][$field_id])){
        foreach ($_POST['item_meta'][$field_id] as $p){
          if(is_numeric($p))
            wp_delete_attachment($p, true);
        }
      }else if(is_numeric($_POST['item_meta'][$field_id])){
        wp_delete_attachment( $_POST['item_meta'][$field_id], true );
      }
    }
  }
}
Formidable Hook - Helpful

Insert form data into second database table

Submitted by  — 7 years ago

add_action('frm_after_create_entry', 'copy_into_my_table', 20, 2);
function copy_into_my_table($entry_id, $form_id){
  if($form_id == 4){ //change 4 to the form id of the form to copy
    global $wpdb;
    $values = array('col_name1' => $_POST['item_meta'][25], 'col_name2' => $_POST['item_meta'][26]);
    //replace 25 and 26 with the field ids of the Formidable form. Change col_name to the column names in your table
    $wpdb->insert('table_name', $values);
  }
}
Formidable Hook - Helpful

Decrease an available count in another form

Submitted by  — 7 years ago

This example assumes you have two forms set up: one for the event and one for reservations. The event form will include a field for the number of available openings. The reservation form will include a Dynamic field to list all the events. This function will drop the number of available openings for an event when it is selected.

add_action('frm_after_create_entry', 'after_entry_created', 30, 2);
function after_entry_created($entry_id, $form_id){
  if($form_id == 5){ //change 5 to the ID of your reservations form
    global $wpdb;
    $reward_ids = $_POST['item_meta'][25]; //change 25 to the ID of your Dynamic dropdown field in your reservations form
    $seat_count_field = 15; //change 15 to the ID of the available seats field in the event form
    foreach ( (array) $reward_ids as $reward_id ) {
        $available = FrmEntryMeta::get_entry_meta_by_field( $reward_id, $seat_count_field, true );
        $wpdb->update( $wpdb->prefix .'frm_item_metas', array( 'meta_value' => ( (int) $available-1 ) ), array( 'item_id' => $reward_id, 'field_id' => $seat_count_field ) );
    }
  }
}
Formidable Hook - Helpful

Update or create another entry

Submitted by  — 7 years ago

Use this code to check if the current user already has an entry in Form B when Form A is submitted. If the user does NOT have an entry in that form, this code will create a new entry in Form B. If they do have an entry in Form B, it will update the entry. This example specifically will populate a field in Form B with the user's total number of submissions in Form A.

add_action('frm_after_create_entry', 'update_or_create_entry', 30, 2);
add_action('frm_after_update_entry', 'update_or_create_entry', 10, 2);
function update_or_create_entry($entry_id, $form_id){
     if ( $form_id == 430 ) {//Change 430 to the ID of Form A
         global $wpdb;
         
         $form2 = '480';//Change 480 to the ID of Form B
         //Get user and their total
         $user = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->prefix}frm_items WHERE id=%d", $entry_id));
         if ( !$user ) {
             return;
         }
         $total = FrmProStatisticsController::stats_shortcode(array('id' => 47914, 'type' => 'count', 'user_id' => $user));//Change 47914 with the ID of the field that you want to total
         
         //See if this user already has an entry in second form
         $entry_id = $wpdb->get_var("Select id from {$wpdb->prefix}frm_items where form_id='" . $form2 . "' and user_id=". $user);
         if ( $entry_id ) {
             //update entry
             $wpdb->update( $wpdb->prefix . 'frm_item_metas', array('meta_value' => $total), array('item_id' => $entry_id, 'field_id' => '48262'));//Change 48262 to the ID of the field you want to populate in Form B
         } else {
             //create entry
             FrmEntry::create(array(
               'form_id' => $form2,
               'item_key' => $user . 'total', //change entry to a dynamic value if you would like
               'frm_user_id' => $user,
               'item_meta' => array(
                 48262 => $total, //change 48262 to the ID of the field you want to populate (in Form B)
                 48269 => $user,//Change 48269 to the ID of the userID field in Form B
               ),
             ));
         }
  }
}
Formidable Hook - Helpful

Clear a field value after email is sent

Submitted by  — 7 years ago

If there are values in your form that you would like included in the email and then removed from your site, you can delete the value.

add_action('frm_after_create_entry', 'after_entry_created', 42, 2);
function after_entry_created($entry_id, $form_id){
  if ( $form_id == 5 ) { //change 5 to the ID of your form
    FrmEntryMeta::delete_entry_meta($entry_id, 30); // change 30 to your field id
  }
}
Formidable Hook - Helpful

Add user meta to user

Submitted by  — 7 years ago

This example adds the id of the entry to a custom field in the user profile.

add_action('frm_after_create_entry', 'add_entry_id_to_user', 30, 2);
function add_entry_id_to_user( $entry_id, $form_id ) {
 if($form_id == 24){ //change 24 to the form id of the form to copy
   $entry = FrmEntry::getOne($entry_id);
   if ( ! $entry->user_id ) {
       return; //don't continue if no user
   }

   update_user_meta( $entry->user_id, 'frm_the_entry_id', $entry_id );
 }
}
Formidable Hook - Helpful

Send submitted entry to another site

Submitted by  — 7 years ago

Sometimes there is a need to send your data on the another site without actually redirecting to it. This will require quite a bit of customization to put together the url needed.

add_action('frm_after_create_entry', 'yourfunctionname', 30, 2);
function yourfunctionname($entry_id, $form_id){
 if($form_id == 5){ //replace 5 with the id of the form
    $args = array();
    if(isset($_POST['item_meta'][30])) //replace 30 and 31 with the appropriate field IDs from your form
      $args['data1'] = $_POST['item_meta'][30]; //change 'data1' to the named parameter to send 
    if(isset($_POST['item_meta'][31]))
      $args['data2'] = $_POST['item_meta'][31]; //change 'data2' to whatever you need
    $result = wp_remote_post('http://example.com', array('body' => $args));
 }
}
Formidable Hook - Helpful

Create entry in form with repeating section

Submitted by  — 7 years ago

Use the code below to create an entry in Form B (that has a repeating section) when an entry in Form A (also has a repeating section) is submitted.

add_action('frm_after_create_entry', 'create_repeating_section_entry', 30, 2);
function create_repeating_section_entry($entry_id, $form_id){
	if ( $form_id == 5 ) { //replace 5 with the id of Form B

		// Format the values for the repeating section
		$repeating_values = array();
		foreach( $_POST['item_meta'][6804] as $k => $r ) {// Change 6804 to the ID of the repeating section in Form A
			if ( $k === 'form' ) {
				$repeating_values[ $k ] = 1019;// Change 1019 to the ID of the repeating section child form in Form B
			} else if ( is_array( $r ) ) {
				foreach ( $r as $i => $v ) {
					if ( $i == 6806 ) {// Change 6806 to the ID of a field inside of the repeating section in Form A
						$repeating_values[ $k ][12823] = $v;// change 12823 to the ID of a field inside the repeating section in Form B
					} else if ( $i == 6807 ) {// Change 6806 to the ID of a field inside of the repeating section in Form A
						$repeating_values[ $k ][12824] = $v;// change 12823 to the ID of a field inside the repeating section in Form B
					}
				}
			}
		}

		//create entry
		FrmEntry::create(array(
			'form_id' => 10,// Change 10 to the ID of Form B
			'item_key' => $_POST['item_key'], //change entry to a dynamic value if you would like
			'item_meta' => array(
				12821 => 'test value', //change 12821 to the ID of the field you want to populate (in Form B)
				12822 => $repeating_values,//Change 12822 to the ID of the repeating section field in Form B
			),
		));
	}
}
Formidable Hook - Helpful

Create a new WordPress Category

Submitted by  — 7 years ago

If you want a separate form for adding categories, you can use this code to create them. If you want to keep the category creation inside the same form that creates posts, this custom code is not needed. Use a "Tags" field instead, and map it to your category in the "create posts" settings tab.

add_action('frm_after_create_entry', 'after_entry_created', 30, 2);
function after_entry_created($entry_id, $form_id){
  if($form_id == 5){ //change 5 to the ID of your form
    if(isset($_POST['item_meta'][25])){ //change 25 to the ID of the field used to add a category
      $category_name = 'category'; //if you're using a custom taxonomy, change this to the correct name
      if(function_exists('term_exists'))
        $term_id = term_exists($_POST['item_meta'][25], $category_name);
      else
        $term_id = is_term($_POST['item_meta'][25], $category_name);
                    
      if(!$term_id)
        $term_id = wp_insert_term($_POST['item_meta'][25], $category_name);
    }
  }
}
Formidable Hook - Helpful

Break post content and View connection

Submitted by  — 7 years ago

If you are using a form to create a post, and you have mapped the post content to a View, you will not be able to edit the post content through your WordPress admin. The post content will be filtered by the View that it is linked to. If you would like to break the connection between the post content and the View so you can edit your post through the back-end, you may add the code below to your site. Please note that when you break the connection between the post content and a View, then any changes made to the View will not affect the post content.

In the code below, replace 156 with the ID of your form.

add_action('frm_after_create_entry', 'frm_break_post_content_connection', 60, 2);
function frm_break_post_content_connection( $entry_id, $form_id ) {
	if ( $form_id == 156 ) {// Replace 156 with the ID of your form
		$entry = FrmEntry::getOne( $entry_id );

		if ( ! $entry->post_id ) {
			return;
		}

		delete_post_meta( $entry->post_id, 'frm_display_id' );
	}
}
If you would like to add your own example, click here.

Have something to add?

Click here to provide feedback on this page.