最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How to check current user before all actions and filters?

programmeradmin3浏览0评论

I am working on a plugin with 50+ actions and filters. All actions and filters only apply to users with a specific user meta (b2b). I want to check this user meta before everything How can I do this? A simple check like get_current_user_id DOES NOT WORK before init.

I also don't want to check this inside very single action and filter function, that seems very very inefficient.

function __construct() {

        // This function user check will not work. What can I do?
        if ($this->is_user_b2b()){

            // Add conversations to My account WooCommerce user menu
            add_filter( 'woocommerce_account_menu_items', array($this, 'b2bking_my_account_add_conversations'), 10, 1 );
            // Add conversations endpoint
            add_action( 'init', array($this, 'b2bking_conversations_endpoint') );
            // Add content to endpoint
            add_action( 'woocommerce_account_conversations_endpoint', array($this, 'b2bking_conversations_endpoint_content') );
            // Add content to individual conversation endpoint
            add_action( 'woocommerce_account_conversation_endpoint', array($this, 'b2bking_conversation_endpoint_content') );
/* 50 MORE ACTIONS AND FILTERS */

I am working on a plugin with 50+ actions and filters. All actions and filters only apply to users with a specific user meta (b2b). I want to check this user meta before everything How can I do this? A simple check like get_current_user_id DOES NOT WORK before init.

I also don't want to check this inside very single action and filter function, that seems very very inefficient.

function __construct() {

        // This function user check will not work. What can I do?
        if ($this->is_user_b2b()){

            // Add conversations to My account WooCommerce user menu
            add_filter( 'woocommerce_account_menu_items', array($this, 'b2bking_my_account_add_conversations'), 10, 1 );
            // Add conversations endpoint
            add_action( 'init', array($this, 'b2bking_conversations_endpoint') );
            // Add content to endpoint
            add_action( 'woocommerce_account_conversations_endpoint', array($this, 'b2bking_conversations_endpoint_content') );
            // Add content to individual conversation endpoint
            add_action( 'woocommerce_account_conversation_endpoint', array($this, 'b2bking_conversation_endpoint_content') );
/* 50 MORE ACTIONS AND FILTERS */
Share Improve this question asked Mar 30, 2020 at 20:02 Stefan SStefan S 192 bronze badges 1
  • The earliest hook that you can use would be set_current_user which runs just before the init hook. – Sally CJ Commented Mar 31, 2020 at 9:59
Add a comment  | 

1 Answer 1

Reset to default 0

I solved the issue by putting all the actions and filters inside plugins_loaded which fires very early. Like this:

add_action('plugins_loaded', function(){
      if ($this->is_user_b2b()){
            add_action(.....
            add_filter(.....
发布评论

评论列表(0)

  1. 暂无评论