wordpress

wordpress过滤评论留言方法

博客经常受到海外机器人的留言刷屏,在网络上找到了方法,刚才亲测可以过滤,特此记录一下,希望对自己和别人有帮助。

wordpress
wordpress

把下面的代码加入到主题模板文件functions.php中就可以了。

/* 评论验证 */
function refused_spam_comments( $comment_data ) {
 if( is_user_logged_in()){ return $comment_data;} //登录用户不验证
 $pattern = '/[一-龥]/u'; //验证是否存在中文
 if(!preg_match($pattern,$comment_data['comment_content'])) {
 err('评论必须含中文!');
}
 if( wp_blacklist_check($comment_data['comment_author'],$comment_data['comment_author_email'],$comment_data['comment_author_url'], $comment_data['comment_content'], $comment_data['comment_author_IP'], $comment_data['comment_agent'] )){
 // header("Content-type: textml; charset=utf-8");
 err('你填写的某项信息或IP地址已被列入黑名单,无法进行评论,请文明评论!');
 } else {
 return $comment_data;
 }
}
add_filter('preprocess_comment','refused_spam_comments');

阅读量:0