Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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!

share|improve this question
1  
retrieve your options before 'init', and register your sidebars within 'widgets_ini' –  onetrickpony Jan 29 '11 at 11:15
    
Thanks! Which hook should I use to retrieve the options before "init"? I added some code above, maybe that illustrates the problem better. –  user2789 Jan 29 '11 at 23:08
    
Looks like your problem is with the get_option_tree function, which is defined in a plugin - not in core. I'm not familiar with that plugin, but you'd have to look and see when that function is defined, and choose a hook after that, or a lower priority action on that same hook. –  goldenapples Jan 30 '11 at 2:03
    
You're right! It works when I get the option without checking for the function! I had assumed that all plugin functions were available at that point of the load order. I'll look at it! The plugin is called "option tree", by the way, and offers a nice interface for theme options. Thanks. –  user2789 Jan 30 '11 at 3:31
add comment

2 Answers

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" ?

share|improve this answer
    
thanks! But I knew that Wordpress is_condition() tags won't work at that point, but I'm testing for a variable that I got from the options table at the top of the functions file. Is that the same as using a WP conditional tag? –  user2789 Jan 28 '11 at 20:29
    
No. That is a different case and should be able to work.paste your code so we could see –  Bainternet Jan 29 '11 at 8:07
    
Hey - thanks. I updated the question with some code... –  user2789 Jan 29 '11 at 23:05
    
answer updated. –  Bainternet Jan 30 '11 at 0:21
    
Thanks. You are right, I didn't get the option, because I tested for the function. My mistake. I assumed all plugin code was available at that point. Thanks for helping me out! –  user2789 Jan 30 '11 at 3:34
add comment

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?

share|improve this answer
    
hey - thanks for your suggestion. I've updated the question with some code, maybe you could have another look at it. –  user2789 Jan 29 '11 at 23:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.