增强版 屏蔽 WordPress 垃圾评论


博客一天能收到好几十个垃圾评论 目测应该都是些机器人灌水

wordpress-spam 增强版 屏蔽 Wordpress 垃圾评论 wordpress 技术 折腾 网站信息与统计

wordpress-spam

虽然有一些检测的插件 (例如 Akismet) 但是 这些评论还是会存在数据库中 等待过期自动被删除或者你手动删除, 我有点强迫症 一看这么多非得手动去一个一个点 非常的累

LEONA 提供了一个非常好的解决方案 可惜不支持 pingback, trackback (就是文章引用记录, 我觉得是可以保留的) PING-BACK 的时候 XML-RPC需要打开.

原理很简单 wordpress 提供 preprocess_comment 的过滤 然后你需要对传进来的 评论数据进行判断(如果用户真实评论的话会至少按一键这样就触发onkeyup事件) 如果认为是垃圾评论就 使用 wp_die() 提前结束.

WP 官网说 $comment_data 包括了

1
2
3
4
5
6
7
'comment_post_ID'      - The post to which the comment will apply
   'comment_author'       - (may be empty)
   'comment_author_email' - (may be empty)
   'comment_author_url'   - (may be empty)
   'comment_content'      - The text of the proposed comment
   'comment_type'         - 'pingback', 'trackback', or empty for regular comments
   'user_ID'              - (empty if not logged in)
'comment_post_ID'      - The post to which the comment will apply
   'comment_author'       - (may be empty)
   'comment_author_email' - (may be empty)
   'comment_author_url'   - (may be empty)
   'comment_content'      - The text of the proposed comment
   'comment_type'         - 'pingback', 'trackback', or empty for regular comments
   'user_ID'              - (empty if not logged in)

那么我们就可以丰富这个判断内容 即支持 pingback/trackback 又可以屏蔽垃圾代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
$leonax_magic_lower = 328;
$leonax_magic_upper = 3450709;
 
function leonax_anti_spam_form($fields){
    global $leonax_magic_lower, $leonax_magic_upper;
    $leonax_magic = mt_rand($leonax_magic_lower, $leonax_magic_upper);
    $fields['leonax_magic'] = <<<EOT
        <input type="hidden" id="leonax-magic" name="leonax-magic" value="0">
        <script type="text/javascript">
          jQuery(function() {
            jQuery("#comment").on("keyup", function() {
              jQuery("#leonax-magic").val("$leonax_magic");
            });
          })
        </script>
EOT;
    return $fields;
}
 
add_filter('comment_form_default_fields', 'leonax_anti_spam_form');
 
function leonax_anit_spam_caught() {
  wp_die('<strong>ERROR</strong>: Looks like you are a spam bot. Please stop doing this.');
}
 
function leonax_anti_spam_check( $commentdata ) {
// ---以下为修改的----
  $comment_type = '';
  if ( isset($commentdata['comment_type']) ) {
    $comment_type = trim($commentdata['comment_type']);
  }   
  
 if ( ($comment_type == 'pingback') || ($comment_type == 'trackback') ) {
    return $commentdata;
  }
  $content = '';
  if ( isset($commentdata['comment_content']) ) {
    $content = trim($commentdata['comment_content']);
  }   
  if (!strlen($content)) {
    leonax_anit_spam_caught();
  }
  
  if (preg_match("/[a-e0-9]{25,}/i", $content)) { //  人为测试捕捉 上图奇怪的ID
    leonax_anit_spam_caught();  
  }
 
// ---修改结束---------
  global $leonax_magic_lower, $leonax_magic_upper;  
  
  if ( isset($commentdata['user_ID']) && $commentdata['user_ID'] ) { // 登陆用户允许
    return $commentdata;
  }
  
  if ( !isset($_POST['leonax-magic']) ) {
    leonax_anit_spam_caught();
  }
  $magic = intval($_POST['leonax-magic']);
  if ($magic < $leonax_magic_lower || $magic > $leonax_magic_upper) {
    leonax_anit_spam_caught();
  }
  return $commentdata;
}
 
add_filter( 'preprocess_comment' , 'leonax_anti_spam_check' );
$leonax_magic_lower = 328;
$leonax_magic_upper = 3450709;

function leonax_anti_spam_form($fields){
    global $leonax_magic_lower, $leonax_magic_upper;
    $leonax_magic = mt_rand($leonax_magic_lower, $leonax_magic_upper);
    $fields['leonax_magic'] = <<<EOT
        <input type="hidden" id="leonax-magic" name="leonax-magic" value="0">
        <script type="text/javascript">
          jQuery(function() {
            jQuery("#comment").on("keyup", function() {
              jQuery("#leonax-magic").val("$leonax_magic");
            });
          })
        </script>
EOT;
    return $fields;
}
 
add_filter('comment_form_default_fields', 'leonax_anti_spam_form');

function leonax_anit_spam_caught() {
  wp_die('<strong>ERROR</strong>: Looks like you are a spam bot. Please stop doing this.');
}
 
function leonax_anti_spam_check( $commentdata ) {
// ---以下为修改的----
  $comment_type = '';
  if ( isset($commentdata['comment_type']) ) {
    $comment_type = trim($commentdata['comment_type']);
  }   
  
 if ( ($comment_type == 'pingback') || ($comment_type == 'trackback') ) {
    return $commentdata;
  }
  $content = '';
  if ( isset($commentdata['comment_content']) ) {
    $content = trim($commentdata['comment_content']);
  }   
  if (!strlen($content)) {
    leonax_anit_spam_caught();
  }
  
  if (preg_match("/[a-e0-9]{25,}/i", $content)) { //  人为测试捕捉 上图奇怪的ID
    leonax_anit_spam_caught();  
  }

// ---修改结束---------
  global $leonax_magic_lower, $leonax_magic_upper;  
  
  if ( isset($commentdata['user_ID']) && $commentdata['user_ID'] ) { // 登陆用户允许
    return $commentdata;
  }
  
  if ( !isset($_POST['leonax-magic']) ) {
    leonax_anit_spam_caught();
  }
  $magic = intval($_POST['leonax-magic']);
  if ($magic < $leonax_magic_lower || $magic > $leonax_magic_upper) {
    leonax_anit_spam_caught();
  }
  return $commentdata;
}
 
add_filter( 'preprocess_comment' , 'leonax_anti_spam_check' );

我们测试一下 在不登陆 WP的情况下 输入 评论含有 至少20个小写字母和数字构成的这样标识 就被认为是 垃圾评论 (但是不会出现在数据库里 所以你不会看到在等待审核里 直接就被干掉了)

wordpress-spam-detect 增强版 屏蔽 Wordpress 垃圾评论 wordpress 技术 折腾 网站信息与统计

wordpress-spam-detect

使用方法就是把上面的代码 复制到 模板函数文件 functions.php (如果有子模板就放在子模板里)

效果非常的好!

英文: https://helloacm.com/the-best-efficient-anti-spam-php-code-detection-for-wordpress/

GD Star Rating
loading...
本文一共 392 个汉字, 你数一下对不对.
增强版 屏蔽 WordPress 垃圾评论. (AMP 移动加速版本)
上一篇: 怎么选择 crontab 的编辑器?
下一篇: 增加两API: cal 和 uptime

扫描二维码,分享本文到微信朋友圈
c5f2dbde6f1b16fbffaf44782aa23f76 增强版 屏蔽 Wordpress 垃圾评论 wordpress 技术 折腾 网站信息与统计

11 条评论

评论