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_after_update_entry

Last Updated: June 11, 2018

Knowledge Base → Extend Formidable Forms → Formidable Hooks - for Developers → Entry Management → frm_after_update_entry
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 allows you to do something with the data entered in a form after it is updated.

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

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
               ),
             ));
         }
  }
}
  • Usage
  • Parameters
  • Examples
    • Change a user's role
    • Automatically update a field in another form
    • Update or create another entry
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.

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