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
|
1 Answer
Reset to default 0I 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(.....
set_current_user
which runs just before theinit
hook. – Sally CJ Commented Mar 31, 2020 at 9:59