验证联系表单7中的文本字段,但不起作用

时间:2016-02-08 11:19:42

标签: wordpress-plugin contact-form-7

这是我的代码,但它不起作用

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;
}

任何建议请...帮助我完成

1 个答案:

答案 0 :(得分:1)

考虑到您使用的是最新版本的插件: 尝试以下......

<?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')
  )));
  }
?>