Update:我小看了这个插件,只是解决了一个小问题,但最终结果是未解决。。。。。

Willin大师的小墙广为传播,我一直有知,不过抱着能懒就懒的念头,一直都没有去使用,而是使用着Akismet。

不过,随着评论的增加,貌似误杀率在提高,所以决定换了。

方法倒是简单,直接将Willin提供的以下代码copy到functions.php文件中即可。

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
/* <<小牆>> Anti-Spam v1.81 by Willin Kan. */
//建立
class anti_spam {
  function anti_spam() {
    if ( !current_user_can('level_0') ) {
      add_action('template_redirect', array($this, 'w_tb'), 1);
      add_action('init', array($this, 'gate'), 1);
      add_action('preprocess_comment', array($this, 'sink'), 1);
    }
  }
  //設欄位
  function w_tb() {
    if ( is_singular() ) {
      ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=([\"\'])comment([\"\'])(.+)/textarea>#",
      "textarea$1name=$2w$3$4/textarea><textarea name=\"comment\" cols=\"100%\" rows=\"4\" style=\"display:none\"></textarea>",$input);'
) );
    }
  }
  //檢查
  function gate() {
    if ( !empty($_POST['w']) && empty($_POST['comment']) ) {
      $_POST['comment'] = $_POST['w'];
    } else {
      $request = $_SERVER['REQUEST_URI'];
      $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER']         : '隱瞞';
      $IP      = isset($_SERVER["HTTP_VIA"])     ? $_SERVER["HTTP_X_FORWARDED_FOR"]. ' (透過代理)' : $_SERVER["REMOTE_ADDR"];
      $way     = isset($_POST['w'])              ? '手動操作'                       : '未經評論表格';
      $spamcom = isset($_POST['comment'])        ? $_POST['comment']                : null;
      $_POST['spam_confirmed'] = "請求: ". $request. "\n來路: ". $referer. "\nIP: ". $IP. "\n方式: ". $way. "\n內容: ". $spamcom. "\n -- 記錄成功 --";
    }
  }
  //處理
  function sink( $comment ) {
    if ( !empty($_POST['spam_confirmed']) ) {
      if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment; //不管 Trackbacks/Pingbacks
      //方法一: 直接擋掉, 將 die(); 前面兩斜線刪除即可.
      //die();
      //方法二: 標記為 spam, 留在資料庫檢查是否誤判.
      add_filter('pre_comment_approved', create_function('', 'return "spam";'));
      $comment['comment_content'] = "[ 小牆判斷這是Spam! ]\n". $_POST['spam_confirmed'];
    }
    return $comment;
  }
}
$anti_spam = new anti_spam();
// -- END ----------------------------------------

一般来说,这样就可以了。

不过由于我使用的是iNove主题,没有提供评论嵌套功能,所以我使用了wordpress-thread-comment插件,于是在启用小墙的过程中发生了意外,提交评论时提示说评论为空。

因为小墙是将原评论框的name由comment改为w,然后启用一个隐藏的name为comment的textarea,实际上我们是在w中评论,然后再赋值到comment中;而机器spam是直接填到comment的textarea,故暴露了其为spam的真相。(故对人肉spam无效)

而wordpress-thread-comment会在提交时对name为comment的textarea的内容进行判断,若为空则警告。

所以,解决方案是将wordpress-thread-comment插件目录下的wordpress-thread-comment.js.php文件中的以下代码注释掉:

1
2
3
4
5
6
if(comment == null || comment == ""){
    alert("comment can not be empty");
    if(Commentarea && Commentarea.display != "none")
        Commentarea.focus();
    return false;
}

Update

嗯,修改评论框的name属性,由于使用了wordpress-thread-comment的原因,所以可能会造成很多问题,若有发现,还请各位提醒一下。
看来,还是尽快抛弃这个插件,使用原生的嵌套比较好。

沮丧地再次Update

我把问题想得太简单了,进行嵌套回复时好像wordpress-thread-comment得到的是原来的评论框,所以造成诸多问题,暂时又撤回去了。o(╯□╰)o