好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

WordPress将垃圾评论阻挡在门外 - WordPress

WordPress将垃圾评论阻挡在门外

垃圾评论是各位wordpress博客主的一个头痛的问题当然也包括了我了,现在我整理了一些关于垃圾评论的防止方法,希望这些代码能帮助各位解决头痛之事呀.

今天,翼帆远航将介绍两种把垃圾评论阻挡在门外的方法。

一般情况下,我们会使用著名的反垃圾评论插件:Akismet 来阻止垃圾评论的入侵,Akismet 会将所有识别出的垃圾评论分离出来,并提供一键清理垃圾评论的功能,虽然 Akismet 几乎能够 100% 识别出垃圾评论,但由于每天的垃圾评论实在太多,天天点[一键清理]也不是办法,有没有更加实用的解决方案,能将垃圾评论在提交之前先拦截下来呢?

我们先来分析垃圾评论的类型:

纯英文或含有日文、韩文等他国语言的垃圾评论

固定某个网站用于推广的垃圾评论

对于这两类垃圾评论,我们对症下药,使用两种方法,分别将这两类垃圾评论阻挡在门外。

 

方法一: 不允许纯英文或包含日文、韩文等他国语言的评论提交

将以下代码插入主题目录 functions.php 的合适位置:

// 禁止全英文和日文评论   function  v7v3_comment_post(  $incoming_comment  ) {       $pattern  =  '/[一-?]/u' ;       $jpattern  = '/[ぁ-ん]+|[ァ-ヴ]+/u' ;       if (!preg_match( $pattern ,  $incoming_comment [ 'comment_content' ])) {          err(  "写点汉字吧,博主外语很捉急! Please write some chinese words!"  );      }       if (preg_match( $jpattern ,  $incoming_comment [ 'comment_content' ])){          err(  "日文滚粗!Japanese Get out!日本?出て行け!"  );      }       return (  $incoming_comment  );  }  add_filter( 'preprocess_comment' ,  'v7v3_comment_post' ); 

温馨提示: 若使用以上代码出现异常,请尝试将第 6 行和第 9 行的 err 换成 wpdie。

方法二: 阻止含有违禁关键字的评论提交

将以下代码插入主题目录 functions.php 的合适位置:

// 禁止包含违禁关键字的评论提交到数据库   function  v7v3_fuckspam( $comment )   {       if  (is_user_logged_in())      {            return   $comment ;      }         if  (wp_blacklist_check(                               $comment [ 'comment_author' ],                               $comment [ 'comment_author_email' ],                               $comment [ 'comment_author_url' ],                                $comment [ 'comment_content' ],                                $comment [ 'comment_author_IP' ],                                $comment [ 'comment_agent' ]                           ))      {          header( "Content-type: text/html; charset=utf-8" );          err('                  您的评论包含违禁关键字,或者您的IP已被加入黑名单,如有疑问请联系管理员处理!              ');      }         else         {           return   $comment ;      }  }  add_filter( 'preprocess_comment' ,  'v7v3_fuckspam' ); 

温馨提示: 若使用以上代码出现异常,请尝试将第 6 行和第 9 行的 err 换成 wpdie。

违禁关键字的设置在[仪表盘——设置——讨论——评论黑名单],一个违禁关键字占一行,此方法依旧是来自 Willin Kan 大师(可惜他已经退出WordPress圈),折腾很简单,直接将下面的代码放到主题的functions.php文件的最后一个 ?>前面即可

代码如下:

/ 垃圾评论拦截  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 empty ( $_POST [ 'w' ]) &&  empty empty ( $_POST [ 'comment' ]) ) {      $_POST [ 'comment' ] =  $_POST [ 'w' ];    }  else  {      $request  =  $_SERVER [ 'REQUEST_URI' ];      $referer  = isset( $_SERVER [ 'HTTP_REFERER' ])         ?  $_SERVER [ 'HTTP_REFERER' ]         :  '隐瞒' ;      $IP       = isset( $_SERVER [ "HTTP_X_FORWARDED_FOR" ]) ?  $_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 empty ( $_POST [ 'spam_confirmed' ]) ) {      if  ( in_array(  $comment [ 'comment_type' ],  array ( 'pingback' ,  'trackback' ) ) )  return   $comment ;      //方法一: 直接挡掉, ? die(); 前面两斜线?h除即可.       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(); 

查看更多关于WordPress将垃圾评论阻挡在门外 - WordPress的详细内容...

  阅读:43次