飞流 发表于 2025-10-9 22:46:44

子比主题 – 评论正则拦截验证代码

这是一款子比主题评论正则拦截验证代码,什么意思呢,当我们让用户评论的时候如果评论了纯数字、纯表情、纯英文等等,那么他会不让用户评论,必须带有中文的,话不多说喜欢自行部署!

代码部署
定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创建一个,记得加上php头,要不然会报错,将下面的代码放到里面即可!
//屏蔽纯英文评论
function refused_english_comments($incoming_comment) {
    // 获取评论内容
    $comment_content = $incoming_comment['comment_content'];
   
    // 去除评论内容中的 xxx为任意字符串
    $comment_content = preg_replace('/\]*\]/', '', $comment_content);
   
    // 检查评论内容是否为空
    if (empty($comment_content)) {
      wp_die('{"error":1,"ys":"danger","msg":"评论不能是纯表情内容 <br/>Comments cannot be purely emoji content"}');
    }
   
    // 检查评论内容是否包含中文
    $pattern = '/[一-龥]/u';
   
    // 禁止全英文评论
    if (!preg_match($pattern, $comment_content)) {
      wp_die('{"error":1,"ys":"danger","msg":"评论必须包括中文 <br/>Comments must include Chinese"}');
    }
   
    return $incoming_comment;
}

add_filter('preprocess_comment', 'refused_english_comments');

页: [1]
查看完整版本: 子比主题 – 评论正则拦截验证代码