This blog is about how to validate text fields which can be used as coupon codes or any standard method and even with this method you can even control spam entries.
Paste the below code to the fincation.php file and I will recommend first creating a child theme and then pasting the code into the child theme function.php file.
/**
* Add coupon code field validation or text field validation.
*
*/
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' ] ) !== 17177 ) {
return $fields;
}
//get the value of the coupon code field the user entered
$coupon = $fields[8][ 'value' ];
//coupon code array, each coupon separated by comma
$coupon_code_list = array(
'Code1',
'Code2',
'Code3',
);
// 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' ] ] [ '8' ] = 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 );
Good morning. Please how do you do this same thing for ultimate member. (How to Add Coupon Code or Text Field Validation on ultimate member)