Check out high-converting landing pages for lead generation. Explore   LandingLayouts.com

HomeBlogWordPressUpdate field value before submission in WPForms

Update field value before submission in WPForms

Recently, I have completed a WPForms customization project, where the client’s requirement was to generate a random 8-digit order number and added into a WPFroms hidden field and it must be done using a filter hook with no javascript.

So I have used wpforms_process_filter filter hook to update field hidden field value programmatically before submission.

How wpforms_process_filter works?

The “wpforms_process_filter” hook in the WPForms plugin allows you to modify the form data before it is submitted. You can use this hook to change the value of a specific field before the form is processed.

This filter includes three parameters:

$fields – (array) Sanitized entry field values/properties.
$entry – (array) Original $_POST global.
$form_data – (array) Form settings/dataUwpforms_process_filter

Usage Example wpforms_process_filter

Simply, I have added a simple contact form and a hidden field named ‘Request ID’.

Insert Hidden Field on WPForms
Insert Hidden Field on WPForms
Note down the field ID and form ID
Note down the field ID and form ID

As we have the form id and field id we will use these ids to target the correct form so the filter hook only takes effect on the form that we need.

Now, we have to insert the below code into the active theme’s functions.php file. Make sure to change the form id and field id accordingly.

/**
 * Update field value before submission in WPForms
 *
 * @link  https://iamavi.com/update-field-value-before-submission-in-wpforms/
 *
 * @param  array  $fields     Sanitized entry field values/properties.
 * @param  array  $entry      Original $_POST global.
 * @param  array  $form_data  Form data and settings.
 *
 * @return array
 */

function av_wpforms_process_filter( $fields, $entry, $form_data ) {
    //  Validate the form ID
	if($form_data['id'] != 5804){
        return $fields;
    }

    // Generate 8 digit random number
    $request_id = rand(10000000,99999999);

    // Update the hidden field value
    $fields[3]['value'] = $request_id;

    return $fields;
}
add_filter( 'wpforms_process_filter', 'av_wpforms_process_filter', 10, 3 );

Now, I will change the confirmation message and set the generated Request ID, and once the form submission is complete it will show the Request ID.

Modify WPForms confirmation message
Modify the WPForms confirmation message

Well, now time to test the form.

Test WPForms and check that hidden field value showing or not
Test WPForms and check whether the hidden field value showing or not

Hope this article helped you change the field value before submission on WPForms.


Need Help With WPForms Customization?

If you need an experienced WordPress developer to assist you with customizing your forms using the plugin WPForms, you can reach out to me directly.


Leave a Reply

Your email address will not be published. Required fields are marked *