Hooking a function onto the sidebar?

Load at the sidebar bottom

In most of the sidebars, you’ll find the call to the wp_meta() action hook, where you can hook into the (mostly bottom) of a sidebar.

Load on top of your sidebar

The function get_sidebar( $name ) calls the sidebar you want in your template (this allows to have different sidebars). If you want to add stuff to eg. the top of your sidebar, you’re free to use the internal action hook that runs top of your sidebar, right before the sidebar gets loaded You can then hook everything into this call.

Example:

function add_before_my_siderbar( $name ) 
{
    echo "Loaded on top of the {$name}-sidebar";

    // Example that uses the $name of the sidebar as switch/trigger
    'main' === $name AND print "I'm picky and only echo for special sidebars!";
}
add_action( 'get_sidebar', 'add_before_my_siderbar' );

Notes about themes

The get_sidebar-hook is on top of the get_sidebar() function and triggers before the sidebar file gets included. This means, you should use this hook to add content before and wp_meta() to add something after the sidebar. If a theme is using wp_meta() earlier than the end of sidebar template, then it’s doing it wrong.

Leave a Comment