Mainak Ray
Mainak Ray

Reputation: 55

Validate a text field in contact form 7 and its not working

Here is my code , but its not working

add_filter( 'wpcf7_validate_text', 'custom_text_validation_filter', 10, 2 );

function custom_text_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );     
if ('couponcode' == $tag->name) {
$the_value = $_POST[$name];
        if($the_value!="abc")
        {
$result->invalidate( $tag, "Invalid Coupon Code" );
        }
    }

    return $result;
}

Any Suggestion please ...Help me through it

Upvotes: 0

Views: 3879

Answers (1)

PHPExpert
PHPExpert

Reputation: 943

Considering you are using latest version of plugin: Give a try with following...

<?php 
 add_filter('wpcf7_validate_text', 'your_validation_filter_func', 999, 2);
 add_filter('wpcf7_validate_text*', 'your_validation_filter_func', 999, 2);
 function your_validation_filter_func($result, $tag) {
    $type = $tag['type'];
    $name = $tag['name'];
    if ('coupon_code' == $name) {
    $the_value = $_POST[$name];
    if($the_value != "abc"){
     $result->invalidate($tag, wpcf7_get_message('Invalid_coupon_code'));
     }
   }
  return $result;
}

add_filter('wpcf7_messages', 'customwpcf7_text_messages');

function customwpcf7_text_messages($messages) {
    return array_merge($messages, array(
'invalid_coupon_code' => array(
     'description' => __("Coupon is invalid", 'contact-form-7'),
     'default' => __('Coupon seems invalid.', 'contact-form-7')
  )));
  }
?>

Upvotes: 3

Related Questions