How do I hook a sidebar via add_action?

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.

Leave a Comment