WordPress自动禁止包含特定关键词的垃圾评论
其实过滤特定关键词垃圾评论很简单,先我们定义一个关键词文件,然后当用户提示评论时我们遍历关键词文件进行strstr判断即可实现了.
方法简介: 将以下代码添加到WordPress博客主题的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 ( '不要乱发' , 'baidu.com' , 'www.phpfensi.com' , 'www.phpfensi.com' , 'is' ); 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' );现在全英文的垃圾评论也不洗,我们可以限制一下.
刚一发我就扒过来了~~~~来自v7v3修改自知更鸟,一般的spammer都是外国人,第一次发布绝对是全英文,所以第一次可以挡住80%的垃圾评论!根据email来判断是否为同一人,如果是,并且第一次没有使用全英文评论,即可正常发言不受限制,再加上常见的敏感关键词,多数的英文、日文评论都不在话下,放至主题文件中functions.php:
function v7v3_en( $comment ) { $pattern = '/[一-?]/u' ; $cau = $comment [ 'comment_author' ] ; $cem = $comment [ 'comment_author_email' ] ; global $wpdb ; $ok_to_comment = $wpdb ->get_var( "SELECT comment_approved FROM $wpdb->comments WHERE comment_author = '$cau' AND comment_author_email = '$cem' and comment_approved = '1' LIMIT 1" ); if ( is_user_logged_in() || 1 == $ok_to_comment ){ return $comment ; } elseif ( !preg_match_all( $pattern , $ccontent , $match ) ) { exit (' <head><meta http-equiv= "Content-Type" content= "text/html; charset=utf8" /></head> 初次评论不允许纯英文哦~<a href= "javascript:history.go(-1);" >向上一页</a>'); } } add_filter( 'preprocess_comment' , 'v7v3_en' ); function v7v3_comment_post( $incoming_comment ) { $http = '/[<|KTV|ッ|の|ン|??|?I|グ|?|]/u' ; if (preg_match( $http , $incoming_comment [ 'comment_content' ])) { wp_die( " <head><meta http-equiv= 'Content-Type' content= 'text/html; charset=utf8' /></head> 您的评论包含敏感关键词,被系统判断为垃圾评论!<a href= 'javascript:history.go(-1);' >向上一页</a>" ); } return ( $incoming_comment ); } add_filter( 'preprocess_comment' , 'v7v3_comment_post' );最后还可以使用wordpress评论黑名单功能
1、登陆Wordpress后台→设置→讨论→评论黑名单(内容可自行修改为合适自己博客的)到评论黑名单,点击保存即可,代码如下:
www.phpfensi.com
原理: 垃圾站收集垃圾评论常见词库,出现此类词汇的垃圾评论一律被Wordpress后台直接消灭!
2、修改Wordpress主题functions.php文件[修改前注意备份文件]
注: 本站使用comments-ajax.php处理提交,用err输出错误信息,如果贵站没有使用comments-ajax.php,那么请用wp_die输出错误信息!
方法: 将下面代码中的err换成wp_die,否则会出现500错误,代码如下:
//评论外链数检测 function BYMT_spamlinks( $comment ) { $spamlinks = preg_match_all( '/<a [^>]*href/i' , $comment [ 'comment_content' ], $out ); if ( $spamlinks >1){ //1为允许的链接数量 err(__( '抱歉,检测到评论外链过多,请重写' )); } else { return $comment ; } } add_filter( 'preprocess_comment' , 'BYMT_spamlinks' );查看更多关于WordPress自动禁止包含特定关键词的垃圾评论 - W的详细内容...