【发布时间】: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