因为自己在博客挂了广告,Akismet 最近发来邮件说,盈利网站需要付费使用插件。一气之下直接禁用了插件。第二天看了一下待审评论700+条。疯了。赶紧网上找找如何治理垃圾评论。

其实网上针对 WordPress 的垃圾评论治理方法还是挺多的,一般是通过插件或者代码实现。自己选用了最简单的一种方法。因为是中文博客,所以评论大部分是国人。只要是评论中没有一个中国字的,那就全部阻挡在外。

将下方代码添加到 functions.php 中即可

/* Spam Comments Filter */
function refused_spam_comments($comment_data) {
    $pattern = '/[一-龥]/u';
    $jpattern = '/[ぁ-ん]+|[ァ-ヴ]+/u';
    if (!preg_match($pattern, $comment_data['comment_content'])) {
        wp_die('您的评论有可能是垃圾评论!Your comments are assumed !');
    }
    if (preg_match($jpattern, $comment_data['comment_content'])) {
        wp_die('您的评论有可能是垃圾评论!Your comments are assumed !');
    }
    return ($comment_data);
}
add_filter('preprocess_comment', 'refused_spam_comments');

/* Spam link exists */
function Shield_link($comment_data) {
    $links = '/http:\/\/|https:\/\/|www\./u';
    if (preg_match($links, $comment_data['comment_author']) || preg_match($links, $comment_data['comment_content'])) {
        wp_die('对不起,请不要发网址链接!!');
    }
    return ($comment_data);
}
add_filter('preprocess_comment', 'Shield_link');

Ref:
https://www.cnblogs.com/kenshinobiy/p/7364793.html
https://zmingcx.com/yi-dong-wp-comments-postspam.html
https://blog.csdn.net/weixin_34190533/article/details/115542245