A-A+

WP Anti Spam 小牆 1.84

2013年01月13日 博客应用 暂无评论 阅读 51 次浏览 次

相信大家对垃圾评论都恨之入骨,我这里有时一天竟然多达几百条,大都是英文(据说这些外文的spam都是出自我们国人的杰作...)虽然Akismet防垃圾留言插件会把大部分spam挡在后台,但可能也会误判部分正常留言,从密密麻麻的垃圾中捡回来,也是非易事。为了挡住这些机器发的spam,有的童鞋很无奈地添加一个验证码功能,但这样会影响真正浏览者的体验,可能会造成博客留言的锐减。还好Willin Kan大师提供的WP Anti Spam 小墙代码,比较完美地解决了这一问题。

防spam功能添加方法:

打开functions.php找到://全部设置结束

  1. /* <<小牆>> Anti-Spam v1.84 by Willin Kan. */ 
  2. class anti_spam {  
  3.   function anti_spam() {  
  4.     if ( !current_user_can('read') ) {  
  5.       add_action('template_redirect', array($this, 'w_tb'), 1);  
  6.       add_action('init', array($this, 'gate'), 1);  
  7.       add_action('preprocess_comment', array($this, 'sink'), 1);  
  8.     }  
  9.   }  
  10.     //設欄位  
  11.   function w_tb() {  
  12.     if ( is_singular() ) {  
  13.       ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=(["'])comment(["'])(.+)/textarea>#",  
  14.       "textarea$1name=$2w$3$4/textarea><textarea name="comment" cols="100%" rows="4" style="display:none"></textarea>",$input);') );  
  15.     }  
  16.   }  
  17.   // 檢查  
  18.   function gate() {  
  19.     $w = 'w';  
  20.     if ( !emptyempty($_POST[$w]) && emptyempty($_POST['comment']) ) {  
  21.       $_POST['comment'] = $_POST[$w];  
  22.     } else {  
  23.       $request = $_SERVER['REQUEST_URI'];  
  24.       $way     = isset($_POST[$w]) ? '手動操作' : '未經評論表格';  
  25.       $spamcom = isset($_POST['comment']) ? $_POST['comment'] : '';  
  26.       $_POST['spam_confirmed'] = "請求: ". $request. "n方式: ". $way. "n內容: ". $spamcom. "n -- 記錄成功 --";  
  27.     }  
  28.   }  
  29.   // 處理  
  30.   function sink( $comment ) {  
  31.     // 不管 Trackbacks/Pingbacks  
  32.     if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment;  
  33.  
  34.     // 已確定為 spam  
  35.     if ( !emptyempty($_POST['spam_confirmed']) ) {  
  36.       // 方法一: 直接擋掉, 將 die(); 前面兩斜線刪除即可.  
  37.       die();  
  38.       // 方法二: 標記為 spam, 留在資料庫檢查是否誤判.  
  39.       // add_filter('pre_comment_approved', create_function('', 'return "spam";'));  
  40.       // $comment['comment_content'] = "[ 小牆判斷這是Spam! ]n". $_POST['spam_confirmed'];  
  41.       // $this->add_black( $comment );  
  42.     } else {  
  43.       // 檢查頭像  
  44.       $f = md5( strtolower($comment['comment_author_email']) );  
  45.       $g = sprintf( "http://%d.gravatar.com", (hexdec($f{0}) % 2) ) .'/avatar/'. $f .'?d=404';  
  46.       $headers = @get_headers( $g );  
  47.       if ( !preg_match("|200|", $headers[0]) ) {  
  48.         // 沒頭像的列入待審  
  49.         add_filter('pre_comment_approved', create_function('', 'return "0";'));  
  50.         //$this->add_black( $comment );  
  51.         }  
  52.     }  
  53.     return $comment;  
  54.   }  
  55.   // 列入黑名單  
  56.   // function add_black( $comment ) {  
  57.     // if (!($comment_author_url = $comment['comment_author_url'])) return;  
  58.     // if (strpos($comment_author_url, '//')) $comment_author_url = substr($comment_author_url, strpos($comment_author_url, '//') + 2);  
  59.     // if (strpos($comment_author_url, '/'))  $comment_author_url = substr($comment_author_url, 0, strpos($comment_author_url, '/'));  
  60.     // update_option('blacklist_keys', $comment_author_url . "n" . get_option('blacklist_keys'));  
  61.   // }  
  62. }  
  63. $anti_spam = new anti_spam();  
  64. // -- END ---------------------------------------- 

为了更彻底地防止spam,按Willin Kan的说法,同时将WP程序根目录中的wp-comments-post.php 文件更名,貌似机器发spam,会直接读取此文件,而不用填写表单,而主题集成了ajax评论特效,正常评论读取的是主题中的comments-ajax.php文件。

标签:

给我留言