解决wordpress评论必须包含中文,防止垃圾评论

当网站有一定访问量时,除了收到正常的评论外,还会收到各种垃圾评论,使用的Wordpress自带的akismet后,效果不是很佳,所谓的垃圾评论多数有一个共同的特点:带链接、纯英文,数字等。怎样才能有效的屏蔽这些垃圾评论呢?

之前本站也收到了外语的垃圾,简单的拉黑了邮箱就好很多了,但是还不够,还要防止不少的水军评论,也加一些限制也是必不可少的,所以将下面的代码加到主题的functions模板文件中,就可以有效屏蔽无中文的垃圾评论:

function lianyue_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
// 禁止全英文评论
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "您的评论中必须包含汉字!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'lianyue_comment_post');

有些评论还会带很多的链接地址,可以通过将以下代码加到主题的functions模板文件中,禁止包含特殊内容( href=” 、rel=”nofollow”、http://)的评论:

function lianyue_comment_post( $incoming_comment ) {
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
if(preg_match($http, $incoming_comment['comment_content'])) {
wp_die( "万恶的发贴机!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'lianyue_comment_post');

仔细看的话会发现,也是可以将两者合在一起,这样既能屏蔽无中文的评论,又能屏蔽包含特殊内容的评论:

function lianyue_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
// 禁止全英文评论
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "您的评论中必须包含汉字!" );
}elseif(preg_match($http, $incoming_comment['comment_content'])) {
wp_die( "万恶的发贴机!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'lianyue_comment_post');

如果你所用的主题使用了ajax评论,错误提示页面可能会出现布局混乱,可以通过以下方法进行解决:

打开comments-ajax.php找到最后个err( __())并在下一行增加:

这是必须包含中文的:

$pattern = '/[一-龥]/u';
if (!preg_match($pattern,$comment_content) )
err( __('您的评论中必须包含汉字!') );

这是禁止包含的内容:

$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';  
			if (preg_match($http,$comment_content) )  
			err( __('万恶的发贴机!') );  

我使用以上方法后,这样你的站点收到的垃圾评论越来越少,效果非常明显。

© 版权声明
THE END
喜欢点个赞支持一下吧
点赞0赏币 分享
评论交流 抢沙发

请登录后发表评论

    暂无评论内容