始终坚信
好记性不如赖笔头!

wp_die函数评论导致页面变形处理办法

近日更换主题后发现恶意评论插件失效了,于是乎从原来的主题文件中COPY过来,发现能用,但是报错后页面变形了,研究后发现是WP_DIE出现问题,现把经验分享下把所有的WP_DIE函数替换为 echo ,结尾处增加:die即可!

// 拦截新读者无中文或有日文垃圾评论、禁止新用户黑名单关键词评论提交。登录用户不受影响。
//如用ajax提交评论 需将 wp_die() 改成 ajax 错误提示函数 如 err。
if ( !is_user_logged_in() ) {
    function refused_spam_comments( $comment_data ) {
        $pattern = '/[一-龥]/u';
        $jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
        $nourl = '/http(?:s?):\/\/((?:[A-za-z0-9-]+\.)+[A-za-z]{2,4})/';
        $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'])) { // 禁止无中文评论
	            echo "<i class='fa fa-exclamation-circle'></i><font color=red>
	            Error: 评论必须含中文!You should type some Chinese word (like “你好”) in your comment to pass the spam-check!评论必须包含汉字!</font>";
	            die;
	            
	            //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'])){  // 禁止日文评论
	    	 echo "<i class='fa fa-exclamation-circle'></i><font color=red>Error: 评论不能包含日文!";
	    	 die;
	    	}
	    	else if(preg_match($nourl, $comment_data['comment_content'])){
	    	     echo "<i class='fa fa-exclamation-circle'></i><font color=red>Error: 评论不能包含网址!";
	    	     die;
	    	}
	    	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'] )){
		        echo "<i class='fa fa-exclamation-circle'></i><font color=red>Error: 评论关键词或者IP黑名单!";// 禁止黑名单提交评论 
		        die;
	    	    
	    	}
	    }
        return( $comment_data );
    }
    add_filter('preprocess_comment','refused_spam_comments');
}
赞(0)
本站未标注原创文章均为转载,如有侵权请告知!二宝博客 » wp_die函数评论导致页面变形处理办法

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址