How do I hook a sidebar via add_action?

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

Read More

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.

Related posts

Leave a Reply

3 comments

  1. 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.

  2. 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' );
    
  3. Sorry to post this as an answer when it should be a comment.
    After struggling for a while with calling a sidebar in a hook problem, I’d like to mention that sidebar ID should be all lower case.
    I you use a capital letter in sidebar ID (ex: ‘Secondary’), the registered sidebar will appear in wp-admin, you may add widgets to it, but if you don’t register the sidebar the associated widgets will not appear as ‘dis-activated widgets’… they just disappear :-/
    is_active_sidebar( ‘Secondary’ ) will always return false
    dynamic_sidebar( ‘Secondary’ ) or get_sidebar( ‘Secondary’ ) will return nothing.

    I’m using wordpress 4.5.2
    Hope this can help someone 🙂

    Have a nice day