How to Perform Field Comparisons Within Your WPForms


Interested in performing field comparisons within your form? While you may already know how to add a confirmation check for the Email field, what if you want to extend this capability to other fields like Phone or Address? Look no further! This tutorial will guide you through the straightforward process of achieving this using a concise PHP snippet.

If you’re interested in doing this for the Email field, please check out this documentation. You can create this comparison right inside the form builder!

Creating the form

To get started, let’s create a fresh form and include the necessary fields. In this tutorial, we’ll focus on crafting a form designed for visitors interested in obtaining a free quote. Consequently, we’ll add specific fields, including two distinct Phone fields: one for entering the phone number and another for confirming it.

create a new form and add your fields including 2 phone fields. one is for entering the number, the other phone field we will use for a confirmation

If you need any assistance in creating a new form, please review this helpful guide.

Adding the snippet

Now it’s time to add the snippet to your site. If you need help with where and how to add snippets, please take a look at this guide.

 * Perform Field Comparisons
 * @link

 function wpf_dev_compare_fields( $fields, $entry, $form_data ) {    
    // Only run this snippet on the form ID 3697
    if ( absint( $form_data[ 'id' ] ) === 3697 ) {
        return $fields;
    // Enter the field ID for the first phone number
    $phone_1 = $fields[5][ 'value' ]; 
	// Enter the field ID for the second phone number 
    $phone_2 = $fields[7][ 'value' ]; 
	// Now compare the phone nubmers and if they do not match, display an error message 
    if ( $phone_2 !== $phone_1 ) {
        wpforms()->process->errors[$form_data[ 'id' ]][ 'header' ] = esc_html__( 'Apologies, the phone numbers do not match. Please confirm the phone numbers.', 'plugin-domain' );
add_action( 'wpforms_process', 'wpf_dev_compare_fields', 10, 3 );

Please note that you’ll need to update the form and field IDs in the snippet to match your own IDs. For any help in finding your ID numbers, please check out this tutorial.

This snippet will look at the form ID 3697 and then grab the values entered on both Phone fields by looking that the field ID 5 and 7, if these fields don’t match, we’ll display an error message asking them to confirm the numbers entered.

using this snippet it is easy to perform field comparisons in your forms

And that’s it! By using field comparisons, you can ensure the data being entered in your forms is verified. Would you like to conditionally display the Submit button? Take a look at our tutorial on How to Conditionally Show the Submit Button.

Action Reference: wpforms_process