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 the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

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

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson

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'] ) );

      • Angela J says

        Hi Bill

        I’m trying to calculate the time difference between two fields.
        Is this possible with something like you’ve written above? Will it calculate minutes?

        • Bill Erickson says

          Assuming your two fields are storing dates or datetimes, then yes, you could use the PHP function strtotime() which converts a "string to time", and subtract the two numbers to find how many seconds are between them. You can then use the core constants MINUTE_IN_SECONDS or HOUR_IN_SECONDS or DAY_IN_SECONDS to convert the seconds into a more usable number format.

          Example:

          $old = 'January 1, 2022';
          $new = 'March 12, 2022';
          $difference = strtotime( $new ) - strtotime( $old ); // seconds between two dates
          $difference_in_days = $difference / DAY_IN_SECONDS; // days between two dates

          Alternatively, you could use use a relative date function to find the difference between two dates. It does the same as above but picks the right units for you. Ex: 1 hour ago, or 2 weeks ago.

          The Genesis theme framework has this function: genesis_human_time_diff().

          $old = 'January 1, 2022';
          $new = 'March 12, 2022';
          $difference = genesis_human_time_diff( strtotime( $old ), strtotime( $new ) );

  4. Chris Scarem says

    Where do I place / insert the code below?

    /**
    * 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 );

    • Bill Erickson says

      Yes, when you go to Forms > Entries and select the entry, you can click “Edit” to update any of the fields in that entry, including hidden fields.

  5. JARED OOSTERHART says

    Thank you for this wonderful little trick. I’m trying to learn how to do this so that it is calculated before the email to the user goes out and can be used to total some of the fields and show them the total of those fields on the email they receive. In this case we are sending a donation confirmation, but the form includes purchased/taxed items as well. We want to be sure they only receive a total of their donation amount, not the total of the entire form. Can this be tweaked to do this little action?

    Thanks again. ๐Ÿ™‚

    • Bill Erickson says

      The code above should be calculating the total before the email goes out. If you add the Total field to your email notification, the calculated total should appear in the email.

      If you look in /wpforms/includes/class-process.php and search for the filter “wpforms_process_filter” you’ll see it comes before the entry is added to the database and the emails are sent: https://a.cl.ly/5zudDAp1

  6. Andrew says

    Hello Bill!

    Awesome solution, I was struggling with this for a while now, so big thank you!

    I’m using your code with quantity and total price, and I’m just wondering: Is it possible to do decimals at the end?

    For example, if the result is 10000, then display 10.000, if the result is 100000 then display 100.000

    Thanks in advance,
    Andrew

  7. Hossain says

    Hello Bill,
    I have added a checkbox field from payment fields in wpforms and a total field to show the total based on checked items. Now I have added a discount coupon field where user will enter a coupon code. If the code is valid, I show a hidden field where I want to display the discounted price based on the current total price. Any idea how I can proceed further?

    • Bill Erickson says

      I recommend contacting WPForms support for guidance here. I don’t work on forms with payment fields so I don’t have experience with that.

  8. Jason says

    Can this same method be used with if then statements to set a hidden fields value based on the selection? ex: dropdown of states, if pa, ny or nj are selected the field would be set to ‘north East’