WP自动评论批准自定义帖子类型 [英] WP auto comment approve for custom post type

查看:135
本文介绍了WP自动评论批准自定义帖子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 xyz 自定义帖子添加了自动批准评论的操作。但是在添加条件 if($ post_type =='course')时,此方法不起作用。我也尝试过过滤器。但是它不起作用。我该如何解决?

I have added the action for auto approve comments for my xyz custom post. But its not working when added the condition if($post_type =='course'). I have tried with also filter. But its not working. How can i solve that?

操作:

global $post_type;
if($post_type =='xyz'){
    function action_pre_comment_approved( $array, $int, $int ) { 

    }; 
add_action( 'pre_comment_approved', 'action_pre_comment_approved', 10, 3 ); 
}

过滤器:

global $post_type;
if($post_type =='xyz'){
    function filter_pre_comment_approved( $approved, $commentdata ) { 

        return $approved; 
    }; 
    add_filter( 'pre_comment_approved', 'filter_pre_comment_approved', 10, 2 ); 
}


推荐答案

尝试以下代码。

function filter_pre_comment_approved( $approved, $commentdata ) {   
    global $post_type;
    if($post_type =='xyz'){
        return $approved; 
    }else{
        return false;
    }
}
add_filter( 'pre_comment_approved', 'filter_pre_comment_approved', 10, 2 ); 

这篇关于WP自动评论批准自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆