From what hook on is it possible to conditionally add actions?

I tried to have a siderbar registered at the “init” hook based on a variable from the theme options array indicating whether the sidebar should be used in the design.

Now it appears that that the condition works fine (tested with some other code) but if I use

Read More

add_action( 'init', 'register_sidebar_x' );
or
add_action( 'wp_loaded', 'register_sidebar_x' );

based on the condition being true, nothing happens, and the sidebar won’t be registered, while it will be registered just using the code outside of the control structure using either hook.

So it appears that the init function is executed before the functions.php has access to the theme options array (which is included). The control structure works with “wp_head”, for example.

So my question is – from what hook on is it possible to use conditionals and is there one that can be used to register sidebars after that point?

UPDATE:

OK, here’s the current code trying to incorporate some suggestions from below… The sidebar is registered just fine if I’m not testing for the condition, but the condition clearly is ‘yes’. Is there any way to conditionally register sidebars?

add_action( 'after_setup_theme', 'aad_theme_setup' );

function aad_theme_setup() {

    if ( function_exists( 'get_option_tree') ) {
        $theme_options = get_option('option_tree');
    }

    if ($theme_options['aad_sliding_sidebar']=='yes') {
        add_action( 'widgets-init', 'aad_register_sidebar_function' );  
    }
}

So it still doesn’t work… thanks for any additional suggestions!

Related posts

Leave a Reply

2 comments

  1. I found this on the codex

    You can only use conditional query tags on or after the init action hook in WordPress. For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.

    update

    you don’t need to hook the register sidebar function just call it like this:

    add_action( 'after_setup_theme', 'aad_theme_setup' );
    
    function aad_theme_setup() {
    
        if ( function_exists( 'get_option_tree') ) {
            $theme_options = get_option('option_tree');
        }
    get_option_tree
        if ($theme_options['aad_sliding_sidebar']=='yes') {
            aad_register_sidebar_function();
        }
    }
    

    but make sure that you can access your option_tree option because you are checking if
    “get_option_tree” function exists but you are using “get_option” and also maybe but just maybe in your code you are calling “aad_register_sidebar_function()” instead of “add_register_sidebar_function()” two “aa” and not two “dd” ?

  2. The after_setup_theme hook is called very early on (right after finding and including the functions.php file), and you should be able to use get_option on that hook.

    init, however, is called before register_sidebars, so I’m not sure why you’re not able to register the sidebars at that point. What are you using to check the options table? Is it a simple get_option function or are you depending on the autoloaded options?