1

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 */
1
  • The earliest hook that you can use would be set_current_user which runs just before the init hook.
    – Sally CJ
    Mar 31, 2020 at 9:59

1 Answer 1

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(.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.