一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

wordpress广告垃圾评论过滤(验证码、关键词)

时间:2013-12-03 编辑:简简单单 来源:一聚教程网

方法一,给评论增加算术验证码

在主题目录的functions.php添加如下代码:

 代码如下 复制代码

    //算术验证码by vfhky
    function spam_provent_math(){
    $a=rand(5,15);
    $b=rand(5,15);
    echo " = $a + $b (防止机器人评论)" ."" ."";
    }
    function spam_provent_pre($spam_result){
    $sum=$_POST['sum'];
    switch($sum){
    case $_POST['a']+$_POST['b']:break;
    case null:wp_die('亲,算个结果撒');break;
    default:wp_die('算错啦⊙?⊙b汗');
    }
    return $spam_result;
    }
    //注册用户or管理员则不需要验证
    if(!is_user_logged_in() && $comment_data['comment_type']==''){
    add_filter('preprocess_comment','spam_provent_pre');
    }

(二)在主题目录下的comments.php(不同的主题可能评论框的位置不同,有的主题可能在functions.php里面)中调用上述代码:

 代码如下 复制代码

    //根据是否是管理员来决定是否显示验证码
 


方法二,利用程序进行关键过滤

在你的主题下面的functions.php里面添加如下代码即可

后台查看垃圾评论

 代码如下 复制代码
    // 单独使用禁止全英文评论代码
    function scp_comment_post( $incoming_comment ) {
    $pattern = '/[一-?]/u'; //验证是否为中文
    
    if(!preg_match($pattern, $incoming_comment['comment_content'])) {
    wp_die( "You should type some Chinese word (like "你好") in your comment to pass the spam-check, thanks for your patience! 您的评论中必须包含中文!" );
    }
    return( $incoming_comment );
    }
    add_filter('preprocess_comment', 'scp_comment_post');
    
    
    //综合使用< <小??>> Anti-Spam v1.84 by Willin Kan.
    class anti_spam {
    function anti_spam() {
    if ( !current_user_can('read') ) {
    add_action('template_redirect', array($this, 'w_tb'), 1);
    add_action('init', array($this, 'gate'), 1);
    add_action('preprocess_comment', array($this, 'sink'), 1);
    }
    }
    // ??谖?br />     function w_tb() {
    if ( is_singular() ) {
    // 非中文?系
    if ( stripos($_SERVER['HTTP_ACCEPT_LANGUAGE'], 'zh') === false ) {
    add_filter( 'comments_open', create_function('', "return false;") ); // ????
    } else {
    ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=(["'])comment(["'])(.+)/textarea>#",
    "textarea$1name=$2w$3$4/textarea>