Update form field values in WPForms

In the GenesisWP Slack, Dan asked a great question about WPForms:

How can I add two WPForms fields together to display the result in the form’s notification?

Dan Brubaker

I would create a hidden field named “Total” and set it to the sum of the two other fields. We can then use a smart tag for displaying the total field’s value in the notification and confirmation message.

wpforms_process_filter

We can use the wpforms_process_filter filter to customize field values right after the form has been submitted, but before it is saved in the database and emails are sent.

This filter includes three parameters

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

Example

My form uses number fields for Red Shirts and Blue Shirts. I also have a hidden field called “Total Shirts”.

We can then update the Total Shirts field value on submission.

I’m using the form ID and field IDs to target the appropriate fields. Alternatively, you could target custom CSS classes added to the form and fields.

/** * WPForms, update total field * @link https://www.billerickson.net/dynamically-update-fields-in-wpforms/ * * @param array $fields Sanitized entry field values/properties. * @param array $entry Original $_POST global. * @param array $form_data Form settings/data * @return array $fields */ function be_wpforms_update_total_field( $fields, $entry, $form_data ) { // Only run on my form with ID = 7785 if( 7785 != $form_data['id'] ) return $fields; // Add red shirts (field ID 3) and blue shirts (field ID 4) into total (field ID 5) $fields[5]['value'] = intval( $fields[3]['value'] ) + intval( $fields[4]['value'] ); return $fields; } add_filter( 'wpforms_process_filter', 'be_wpforms_update_total_field', 10, 3 );

Once the form is submitted, it shows the confirmation message including the total.

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments

  1. Paul says

    This filter is not listed on WPForms and is EXACTLY what I needed!!! Thank you VERY MUCH, you just made my life a lot easier! 🙂

  2. Wayne Philips says

    How can this be modified to take a span ID value from a section on the site outside of the form to then update a hidden field in the form??

    EG we have a custom calculator next to the form and the output is something like 150 – one submit we want to send that result number into a hidden field on the form?

    • Bill Erickson says

      The method above only works with data that’s already inside the form because it is calculated when the form is processed.

      In your case, you’ll need to use JavaScript to update the hidden field value based on your custom calculator’s data.

  3. Justin says

    Hi Bill,

    Thank for that. I’m trying to customise this to calculate and return the age in a hidden field instead. But I’m not a developer so I’m kinda stuck.

    What I’ve done so far is this:

    function be_wpforms_calculate_age_update_field( $fields, $entry, $form_data ) {

    // Only run on my form with ID = 973
    if( 973 != $form_data[‘id’] )
    return $fields;

    $fields[48][‘value’] = intval( $fields[47][‘value’] ) – intval( $fields[5][‘value’] );

    return $fields;
    }
    add_filter( ‘wpforms_process_filter’, ‘be_wpforms_calculate_age_update_field’, 10, 3 );

    But it didn’t work….. and I’m not surprised. Would you be able to enlighten me?

    Thank you.

    • Bill Erickson says

      For doing calculations on dates, you’ll want to convert the string to a “time” with strtotime()

      To find the difference between the two dates, I recommend using human_time_diff( $from, $to ) which will return a relative time like “2 days” or “5 weeks”.

      Ex: $fields[48]['value'] = human_time_diff( strtotime( $fields[47]['value'] ), strtotime( $field[5]['value'] ) );

Leave A Reply