子比zibll主题美化 给评论增加正则拦截验证代码

网站开通评论功能就会有垃圾评论的可能,这样会浪费很多的精力去处理,有些纯英文还有些附带网址,很多插件可以处理这种情况,但插件过多就会影响网站速度,今天就分享个不需要插件来实现屏蔽垃圾评论的功能,那就是给子比加个正则验证,限制评论纯数字/纯英文/纯表情。

使用教程

将代码放到/wp-content/themes/zibll/func.php文件即可。注意 此方法评论必须带中文 禁止了纯数字、纯字母和纯表情。

[hidecontent type=”password” password=”z536x” img_id=”382″ img_url=”https://oss.90bk.com/wp90blog/uploads/2024/03/20240319145541654.jpg” desc=”微信扫描左侧小程序码或搜索【卡密分发助手】小程序,输入【2404221】点领取卡密!密码验证后才能查看!”]
//屏蔽纯英文评论
function refused_english_comments($incoming_comment) {
    // 获取评论内容
    $comment_content = $incoming_comment['comment_content'];

    // 去除评论内容中的 [g=xxx] xxx为任意字符串
    $comment_content = preg_replace('/\[g=[^\]]*\]/', '', $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');
[/hidecontent]