Wordpress自动将包含长链接的评论标记为垃圾评论
现在大量的机器可以直接对WordPress博客进行评论并带有大量的连接,这些带链接评论我们定为垃圾评论了,下面我来给大家介绍几款自动将包含长链接的评论标记为垃圾评论实例.
方法一: 此方法依旧是来自 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();这个方法可以阻止98%以上的垃圾评论,当然了,倡萌还建议你审核第一次提交的评论人的评论,设置审核后才显示,如果遇到垃圾评论,将其email、IP、网址等添加到黑名单即可,下面是倡萌目前的评论设置,你可以在WP后台-设置-讨论下设置.
方法二: 自动拒绝包含特定关键词的垃圾评论,将下面的代码添加到主题的functions.php文件,自己根据需要,修改 $ bad_comment_content 数组的内容,任何包含在$ bad_comment_content 数组内的字符,将会被自动拒绝留言,代码如下:
function in_comment_post_like( $string , $array ) { foreach ( $array as $ref ) { if ( strstr ( $string , $ref )) { return true; } } return false; } function drop_bad_comments() { if (! empty empty ( $_POST [ 'comment' ])) { $post_comment_content = $_POST [ 'comment' ]; $lower_case_comment = strtolower ( $_POST [ 'comment' ]); $bad_comment_content = array ( 'viagra' , 'hydrocodone' , 'hair loss' , 'xanax' , 'tramadol' , 'russian girls' , 'russian brides' , 'lorazepam' , 'adderall' , 'dexadrine' , 'no prescription' , 'oxycontin' , 'without a prescription' , 'sex pics' , 'family incest' , 'online casinos' , 'online dating' , 'cialis' , 'best forex' , 'amoxicillin' ); if (in_comment_post_like( $lower_case_comment , $bad_comment_content )) { $comment_box_text = wordwrap(trim( $post_comment_content ), 80, "n " , true); $txtdrop = fopen ( '/var/log/httpd/wp_post-logger/nullamatix.com-text-area_dropped.txt' , 'a' ); fwrite( $txtdrop , " --------------n [COMMENT] = " . $post_comment_content . "n --------------n" ); fwrite( $txtdrop , " [SOURCE_IP] = " . $_SERVER [ 'REMOTE_ADDR' ] . " @ " . date ( "F j, Y, g:i a" ) . "n" ); fwrite( $txtdrop , " [USERAGENT] = " . $_SERVER [ 'HTTP_USER_AGENT' ] . "n" ); fwrite( $txtdrop , " [REFERER ] = " . $_SERVER [ 'HTTP_REFERER' ] . "n" ); fwrite( $txtdrop , " [FILE_NAME] = " . $_SERVER [ 'SCRIPT_NAME' ] . " - [REQ_URI] = " . $_SERVER [ 'REQUEST_URI' ] . "n" ); fwrite( $txtdrop , '--------------**********------------------' . "n" ); header( "HTTP/1.1 406 Not Acceptable" ); header( "Status: 406 Not Acceptable" ); header( "Connection: Close" ); wp_die( __( 'bang bang.' ) ); } } } add_action( 'init' , 'drop_bad_comments' );今天再补充下,自动将包含长链接的评论标记为垃圾评论,将下面的代码添加到主题的 functions.php 文件即可:
function rkv_url_spamcheck( $approved , $commentdata ) { return ( strlen ( $commentdata [ 'comment_author_url' ] ) > 50 ) ? 'spam' : $approved ; } add_filter( 'pre_comment_approved' , 'rkv_url_spamcheck' , 99, 2 );注意看第二行的 50,根据自己的需要修改这个数值,如果你希望将所有带有链接(不管是否是长链接)的评论内容都自动标记为垃圾评论,将 50 改为 1 即可.
查看更多关于Wordpress自动将包含长链接的评论标记为垃圾评论的详细内容...