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.

On one of my templates I have an action hook 'do_xyz'

I want to attach a custom sidebar to the action hook do_xyz

Here is what I am trying in my theme functions.php file, does not work:

add_action('do_xyz', 'get_sidebar('secondary')' );

What is the proper way of doing this?

I know my hooks are setup right because if I attach a custom built function there, then it works. I just can't seem to attach the sidebar the way I am trying to.

EDIT I have this in my functions file:

function do_xzy() { do_action('do_xyz'); }

Then in my template itself I have:

do_action( 'do_xyz' );

I fully understand registering an action hook in my theme, and also adding a place for the action to occur.

I even somewhat understand add_action, it is just not liking it when I put the get_sidebar('secondary') in there as my function I want to attach to my action hook.

Hope that makes more sense.

share|improve this question
    
i thought 'init' was supposed to be used as the action hook –  Aliyah Jul 26 '12 at 19:13
add comment

2 Answers

up vote 2 down vote accepted

Explanation

There's the global array $wp_filters. This array stores some sub arrays. One for each priority. Each sub array then contains an array for each callback added. And the keys for those sub-sub arrays are the attached functions.

Example

If you do the following, somewhere in your code (preferable after the init hook):

echo '<pre>';
    var_dump( $GLOBALS['wp_filter']['the_content'] );
echo '</pre>';

…then you'll see the following spit out (reduced for the answer):

array
  11 => 
    array
      'capital_P_dangit' => 
        array
          'function' => string 'capital_P_dangit' (length=16)
          'accepted_args' => int 1
      'do_shortcode' => 
        array
          'function' => string 'do_shortcode' (length=12)
          'accepted_args' => int 1
  10 => 
    array
      'wptexturize' => 
        array
          'function' => string 'wptexturize' (length=11)
          'accepted_args' => int 1
      'convert_smilies' => 
        array
          'function' => string 'convert_smilies' (length=15)
          'accepted_args' => int 1
      'convert_chars' => 
        array
          'function' => string 'convert_chars' (length=13)
          'accepted_args' => int 1
      'wpautop' => 
        array
          'function' => string 'wpautop' (length=7)
          'accepted_args' => int 1
      'shortcode_unautop' => 
        array
          'function' => string 'shortcode_unautop' (length=17)
          'accepted_args' => int 1
      'prepend_attachment' => 
        array
          'function' => string 'prepend_attachment' (length=18)
          'accepted_args' => int 1
  8 => 
    array
      '000000002657b7190000000078ed2c6erun_shortcode' => 
        array
          'function' => 
            array
              ...
          'accepted_args' => int 1
      '000000002657b7190000000078ed2c6eautoembed' => 
        array
          'function' => 
            array
              ...
          'accepted_args' => int 1

Use case

So if you're adding the following call to some template:

do_action( 'hook_name' );

…then every callback function, that you attach to this hook, will be saved inside the global array.

// WPSE QuestionNr. Callback Function
function wpse59779_cb_fn()
{
    return print 'I am now added to the template!';
}
add_action( 'hook_name', 'wpse59779_cb_fn' );

So this means that we then have the following output from the var_dump (of the 'hook_name'):

array
  10 => 
    array
      'wpse59779_cb_fn' => 
        array
          'function' => string 'wpse59779_cb_fn' (length=15)
          'accepted_args' => int 1
          ...etc...

What happens?

So if you add a string via add_action to some hook, then you're basically adding data to a global array.

The do_action( 'hook_name' ); then just searches for a function in the global namespace (in this case) and executes the function.

The problem

You're trying to do a lot of things without actually knowing the basics of PHP. Some notes here, but the rest will be on you and php.net:

add_action( 'do_xyz', 'get_sidebar('secondary')' );

This will drop a failure, as you're ending the string inside the callback after the second ', so secondary stands for itself and will maybe considered a constant or just break, as you haven't used a . or , to actually connect things.

How to do it

If you want to add sidebars (or any other element) to a hook, then simply add a callback function that wraps your template tag

function wpse59779_get_sidebars()
{
    return get_sidebar( 'secondary' );
}
add_action( 'do_xyz', 'wpse59779_get_sidebars' );

Advanced

Hooks can take arguments, so you might use a default too:

do_action( 'do_xyz', 'secondary' ); 

Then you can use or modify the default with conditionals for example

function wpse59779_get_sidebars( $sidebar_name )
{
    return get_sidebar( $sidebar_name );
}
add_action( 'do_xyz', 'wpse59779_get_sidebars' );

The example with a conditional:

function wpse59779_get_sidebars( $sidebar_name )
{
    if ( is_page() )
    {
        $sidebar_name = 'page_sidebar';
    }
    elseif ( is_post() )
    {
        $sidebar_name = 'single_post_sidebar';
    }
    return get_sidebar( $sidebar_name );
}
add_action( 'do_xyz', 'wpse59779_get_sidebars' );

Even more Advanced

Hooks can take more than one argument:

do_action( 'do_xyz', 'secondary', 'FFF' ); 

The example with two arguments:

function wpse59779_get_sidebars( $sidebar_name, $color )
{
    echo "<div style='background-color: #{$color};'>Hello!</div>";

    if ( is_page() )
    {
        $sidebar_name = 'page_sidebar';
    }
    elseif ( is_post() )
    {
        $sidebar_name = 'single_post_sidebar';
    }
    return get_sidebar( $sidebar_name );
}
add_action( 'do_xyz', 'wpse59779_get_sidebars', 10, 2 );

You notice, that we now got two more values for the add_action call: The first is the priority (defaults to 10) when to execute the function inside the hook and the second is the number of arguments. Those are only needed if the arguments number is greater than one.

Hope that helps clearing things out.

share|improve this answer
    
Wow, thank you for the awesome explanation –  mike Jul 27 '12 at 7:29
    
You're welcome. Filters and hooks are a little confusing when someone just started over with developing. :) –  kaiser Jul 27 '12 at 10:16
add comment

get_sidebar( 'secondary' ); will load the template file sidebar-secondary.php.

What you're looking for is the dynamic_sidebar() function. You'll have to also register your sidebar first using register_sidebar().

So you have two options.

1) Just put the sidebar in the template file. Put it directly above or below the do_action() call, whichever makes sense:

do_action( 'do_xyz' );
dynamic_sidebar( 'secondary' );

2) Use the hook.

function my_print_sidebar() {
    dynamic_sidebar( 'secondary' );
}
add_action( 'do_xyz', 'my_print_sidebar' );
share|improve this answer
    
thanks mrwweb I had not heard of dynamic_sidbar until now. Is there an advantage to using dynamic_sidebar vs. using return get_sidebar? –  mike Jul 27 '12 at 7:52
    
They're completely different functions. Check out the codex to understand them. dynamic_sidebar() displays the contents of a defined sidebar while get_sidebar() is a template tag for including a template file. Usually they're used in conjunction with each other. –  mrwweb Jul 30 '12 at 15:50
add comment

Your Answer

 
discard

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

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