【问题标题】:Wordpress Contact Form 7 custom validation rule not executedWordpress Contact Form 7 自定义验证规则未执行
【发布时间】:2020-09-02 20:16:57
【问题描述】:

似乎无法让 add_filter() 开始将自定义验证规则添加到联系表单 7 (v5.2.1)。尝试了许多声称可以在本网站和其他网站上使用的示例。但到目前为止似乎没有工作。有人能解释一下吗?

试过下面的代码。

function custom_text_validation_filter($result, $tag) {  
      $type = $tag['type'];  
      $name = $tag['name'];

      if($name == 'your-subject') { 
          $value = $_POST[$name];  
          if (preg_match('/[\'^£$%&*()}{#~><>|=_+¬]/', $value)){

            // $result->invalidate( $tag, "Invalid characters." ); // this did not work

            $result['valid'] = false;
            $result['reason'][$name] = 'Invalid characters';
          } 
      } 
      return $result;  
}
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2); 
add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 999, 2); 

由于上述代码不起作用,请尝试以下操作。

function custom_text_validation_filter($result, $tag) {  
          $type = $tag['type'];  
          $name = $tag['name'];

          if($name == 'your-subject') { 
              $value = $_POST[$name];  
              if (preg_match('/[\'^£$%&*()}{#~><>|=_+¬]/', $value)){

                // $result->invalidate( $tag, "Invalid characters." ); // this did not work
    
                $result['valid'] = false;
                $result['reason'][$name] = 'Invalid characters';
              } 
          } 

    //Added below lines to fire validation error no matter what. But still contact form submits successfully.

          $result['valid'] = false;
          $result['reason'][$name] = 'Invalid characters';
          return $result;  
}
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2); 
add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 999, 2); 

进一步尝试了 add_filter 的不同优先级。

add_filter('wpcf7_validate_text','custom_text_validation_filter', 1, 2); 
add_filter('wpcf7_validate_text','custom_text_validation_filter', 2, 2); 
add_filter('wpcf7_validate_text','custom_text_validation_filter', 10, 2); 
add_filter('wpcf7_validate_text','custom_text_validation_filter', 20, 2); 
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2); 

没有工作

【问题讨论】:

    标签: php wordpress contact-form-7


    【解决方案1】:

    你的工作做得好吗? 参数$tag 在我看来是一个对象。

    除此之外,我看不出有什么阻塞:/

    https://contactform7.com/2015/03/28/custom-validation/

    【讨论】:

    • 是的,我从 $tag->name 开始但没有用。然后发现一些网站说 $tag 已更改为数组并且官方文档是错误的(不知何故)。我也找不到任何其他失败的原因,我现在正在拔头发。
    猜你喜欢
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    相关资源
    最近更新 更多