WordPress禁止非中文及特殊内容评论

2015年6月30日12:22:46网站建设1 717

使用的Wordpress自带的akismet插件后,还常常会受到Wordpress垃圾评论的骚扰,这些垃圾评论多数有一个共同的特点:带链接、英文。怎样才能有效的屏蔽这些垃圾评论呢?文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

作为一个中文站点,很少有老外访问,就算有老外访问,也很少有老外用纯英文评论,所以将下面的代码加到主题的functions模板文件中,可以有效屏蔽无中文的垃圾评论:文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

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');文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

有些评论还会带很多的链接地址,可以通过将以下代码加到主题的functions模板文件中,禁止包含特殊内容( href=" 、rel="nofollow"、http://)的评论:文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

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');文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

当然也可以将两者合在一起,这样既能屏蔽无中文的评论,又能屏蔽包含特殊内容的评论:文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

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');文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

如果你所用的主题使用了ajax评论,错误提示页面可能会出现布局混乱,可以通过以下方法进行解决:文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

打开comments-ajax.php找到最后个err( __(文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

并在下一行增加:文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

$pattern = '/[一-龥]/u';
if (!preg_match($pattern,$comment_content) )
err( __('您的评论中必须包含汉字!') );文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

或者文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
if (preg_match($http,$comment_content) )
err( __('万恶的发贴机!') );文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

我使用以上方法后,站点收到的垃圾评论越来越少,效果非常明显。文章源自堕落的鱼-https://www.duoluodeyu.com/1521.html

匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

评论:1   其中:访客  1   博主  0
    • 一次性毛巾
      一次性毛巾 0

      来看看