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
 * @see 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.

WordPress Development WPForms

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

Leave A Reply