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 allows you to do something with the data entered in a form after it is updated.
Usage
add_action('frm_after_update_entry', 'after_entry_updated', 10, 2); function after_entry_updated($entry_id, $form_id)
Parameters
- $entry_id (integer)
- $form_id (integer)
Examples
Change a user's role
Change a user's role with a dropdown or radio field. Replace 41 with the ID of your form, replace 1775 with the ID of the userID field in the form, and replace 1134 with the ID of the role dropdown or radio field.
add_action('frm_after_update_entry', 'update_user_role', 10, 2);
function update_user_role($entry_id, $form_id){
if ( $form_id == 41 ) {
$userid = $_POST['item_meta'][1775];// 1775 is the ID of the userID field
$role = $_POST['item_meta'][1134];// 1134 is the ID of the role dropdown
if ( $userid && $role ) {
$user = get_userdata( $userid );
if ( $user && ! $user->has_cap('administrator') ) {
$user->set_role( $role );
}
}
}
}
Automatically update a field in another form
Use this code to make sure two fields from two different forms always have the same value stored. For example, when a user updates their Email address in an "Edit Profile" form, then the email address stored in all of that user's entries in a second form will be automatically updated to match the new email address.
add_action('frm_after_create_entry', 'link_fields', 30, 2);
add_action('frm_after_update_entry', 'link_fields', 10, 2);
function link_fields($entry_id, $form_id){
if($form_id == 113){//Change 113 to the ID of the first form
global $wpdb;
$first_field = $_POST['item_meta'][25]; //change 25 to the ID of the field in your first form
$user = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM ". $wpdb->prefix ."frm_items WHERE id=%d", $entry_id));
$entry_ids = $wpdb->get_col("Select id from ". $wpdb->prefix ."frm_items where form_id='112' and user_id=". $user);//Change 112 to the ID of the second form
foreach ($entry_ids as $e) {
$wpdb->update( $wpdb->prefix .'frm_item_metas', array('meta_value' => $first_field), array('item_id' => $e, 'field_id' => '6422'));//Change 6422 to the ID of the field to be updated automatically in your second form
}
}
}
Update or create another entry
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
),
));
}
}
}