How to Add Coupon Code Field Validation on Your Forms


Do you have a coupon field on your form but need to provide some validation for it? Just providing a coupon code for your form is very easy but if you want to make sure it’s a valid coupon before the form is submitted, you’ll just need to add a little PHP snippet and this tutorial will walk your through every step of the way.

Creating your form

Create your form with the fields you need. For the purpose of this tutorial, we’re going to create a new form with the following fields:

  • Name
  • Email
  • Coupon Code (Single Line Text)
  • Checkbox Items (from the Payment Fields)
  • Stripe Credit Card

If you need any help in creating your form, please check out this documentation.

start by creating your form and adding your fields including the single line text field which we will use for our coupon code field validation

You can choose another payment processor such as or PayPal can be used. For the purpose of our tutorial, we’re using the Stripe Credit Card field, if you need assistance in creating a form with this field, please review this documentation.

Adding code for coupon code field validation

Now it’s time to add the code snippet to our site that will validate the coupon field before processing the submission.

If you need help in how and where to add snippets to your site, please see this tutorial.

 * Add coupon code field validation.
 * @link

function wpf_dev_validate_coupon( $fields, $entry, $form_data ) {
    // Optional, you can limit to specific forms. Below, we restrict output to
    // form #164.
    if ( absint( $form_data[ 'id' ] ) !== 164 ) {
        return $fields;
	//get the value of the coupon code field the user entered
	$coupon = $fields[5][ 'value' ];
	//coupon code array, each coupon separated by comma
	$coupon_code_list = array( 
    // check if value entered is not in the approved coupon list array  	
    if (!in_array($coupon, $coupon_code_list)) {  
            // Add to global errors. This will stop form entry from being saved to the database.
            // Uncomment the line below if you need to display the error above the form.
            // wpforms()->process->errors[ $form_data[ 'id' ] ][ 'header' ] = esc_html__( 'Some error occurred.', 'plugin-domain' );    
            // Check the field ID 5 and show error message at the top of form and under the specific field
               wpforms()->process->errors[ $form_data[ 'id' ] ] [ '5' ] = esc_html__( 'Coupon code not found, please confirm the code and try again.', 'plugin-domain' );
            // Add additional logic (what to do if error is not displayed)
add_action( 'wpforms_process', 'wpf_dev_validate_coupon', 10, 3 );

You’ll need to update the $coupon_code_list array by entering the coupon codes you’ll accept. Be sure to separate each coupon with a comma.

You’ll need to update the code snippet above to match your form and field IDs. For help with finding these IDs, please see this tutorial.

Please remember to also update the $coupon_code_list to include all of your accepted coupon codes (separated by a comma).

In the above code snippet, the code will only process on the form ID 164. It will look at the $coupon_code_list array to see if the text entered into the Coupon form field, which is field ID 5. If the code entered isn’t on the list, an error will be shown on the page and the form will not submit.

Now you can see you have coupon code field validation

And that’s it! Would you like to learn how to create a survey for your members that is completely anonymous? Check out our tutorial on How to Create an Anonymous Survey Form for Logged In Users.

Action Reference: wpforms_process