无需插件实现拦截无中文或有日文垃圾评论

下面的代码改自Some Chinese Please插件的核心内容,直接丢主题的functions.php就可以用。
* refused spam */    
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 refuse_comment_jp_post( $incoming_comment ) {
$jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
if(preg_match($jpattern, $incoming_comment['comment_content'])){
wp_die( "禁止有日文字符的评论 You should type some Chinese word" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'refuse_comment_jp_post');
集合:拦截新读者无中文或有日文垃圾评论、禁止新用户黑名单关键词评论提交。
// 拦截新读者无中文或有日文垃圾评论、禁止新用户黑名单关键词评论提交。登录用户不受影响。
//如用ajax提交评论 需将 wp_die() 改成 ajax 错误提示函数 如 err。
if ( !is_user_logged_in() ) {
    function refused_spam_comments( $comment_data ) {
        $pattern = '/[一-龥]/u';
        $jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
        $cau=$comment_data['comment_author'] ;
		$cem=$comment_data['comment_author_email'] ;
		$comment_data['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', "",$_SERVER['REMOTE_ADDR'] );
   		$comment_data['comment_agent'] = isset($_SERVER['HTTP_USER_AGENT'] ) ? substr($_SERVER['HTTP_USER_AGENT'], 0, 254 ) : "";
		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(0 == $ok_to_comment){
	        if(!preg_match($pattern,$comment_data['comment_content'])) { // 禁止无中文评论
	            wp_die( __('Error: 评论必须含中文!You should type some Chinese word (like “你好”) in your comment to pass the spam-check!') );
	        }
	        else if(preg_match($jpattern, $comment_data['comment_content'])){  // 禁止日文评论
	    		wp_die( __('Error: 评论不能包含日文!') );
	    	}
	    	else 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'] )){
		        wp_die( __('Error: 评论关键词或者IP黑名单!') );// 禁止黑名单提交评论 
		    }
	    }
        return( $comment_data );
    }
    add_filter('preprocess_comment','refused_spam_comments');

内容非原创 转自:https://cyhour.com/42/

原创文章,作者:键盘游走者,如若转载,请注明出处:http://www.708034.com/2020/11/wuxuchajianshixianlanjiewuzhongwenhuoyouriwenlajipinglun/

发表评论

邮箱地址不会被公开。 必填项已用*标注