WordPress过滤垃圾评论

2018-06-05 22:49   Tags:

以下内容源自于大自然…

过滤纯英文垃圾评论

function refused_spam_comments( $comment_data ) { 
  $pattern = '/[一-龥]/u'; 
  if(!preg_match($pattern,$comment_data['comment_content'])) { 
  wp_die('评论必须含中文!'); 
} 
  return( $comment_data ); 
} 
add_filter('preprocess_comment','refused_spam_comments');

过滤小鬼子

function fuckjp_comment_post( $incoming_comment ) {
  $http = '/[<|=|.|友|夜|KTV|ッ|の|ン|優|業|グ|貿|]/u';
  if(preg_match($http, $incoming_comment['comment_content'])) {
  wp_die( "错误提示内容.....自定义!" );
}
  return( $incoming_comment );
}
add_filter('preprocess_comment', 'fuckjp_comment_post');

屏蔽长连接评论

function lang_url_spamcheck($approved, $commentdata) {
    return (strlen($commentdata['comment_author_url']) > 50) ?
    'spam' : $approved;
}
add_filter('pre_comment_approved', 'lang_url_spamcheck', 99, 2);

屏蔽昵称,评论内容带链接的评论

function Shield_link($comment_data) {
    $links = '/http:\/\/|https:\/\/|www\./u';
    if (preg_match($links, $comment_data['comment_author']) || preg_match($links, $comment_data['comment_content'])) {
        wp_die('错误提示内容.....自定义!');
    }
    return ($comment_data);
}
    add_filter('preprocess_comment', 'Shield_link');

限制评论最少字数和最多字数

function limit_comment_length( $commentdata ) {
$minCommentlength = 5; //最少字数限制
$maxCommentlength = 200; //最多字数限制
$pointCommentlength = mb_strlen($commentdata['comment_content'],'UTF8'); //mb_strlen 1个中文字符当作1个长度
if ( $pointCommentlength < $minCommentlength )
{
header("Content-type: text/html; charset=utf-8");
wp_die('抱歉,您的评论太短了,请至少输入' . $minCommentlength .'个字(已输入'. $pointCommentlength .'个字)');
exit;
}
if ( $pointCommentlength > $maxCommentlength )
{
header("Content-type: text/html; charset=utf-8");
wp_die('抱歉,您的评论太长了,请少于' . $maxCommentlength .'个字(已输入'. $pointCommentlength .'个字)');
exit;
}
return $commentdata;
}
add_filter( 'preprocess_comment', 'limit_comment_length' );

评论

时隔变迁,评论已自动关闭