I've created the plugin that adds custom post type and custom taxonomis for this post type. Templates for the post type and the taxonomies are located in plugin folder. Now I need to add sidebar for the post type and taxonomies pages. I have the following code to add template for archives page:

// Filter the archives
add_filter('archive_template', 'mytype_archive_template'); 
function mytype_archive_template($archive) {
    global $wp_query, $post; 

// Checks for single template by post type
    if (is_post_type_archive('type')) {
        if (file_exists(plugin_dir_path(__FILE__) . '/archive-mytype.php'))
            return plugin_dir_path(__FILE__) . '/archive-mytype.php';
    return $archive;

That's working good, so I've tried to use the same code for sidebar:

function mytype_get_sidebar($mytype_sidebar) {
// load sidebar template
if (file_exists(plugin_dir_path(__FILE__) . '/sidebar-mytype.php'))
    return plugin_dir_path(__FILE__) . '/sidebar-mytype.php';

// Default return
return $mytype_sidebar;
add_filter('get_sidebar', 'mytype_get_sidebar');

And then on the page where the sidebar should be:

get_sidebar ( apply_filters( 'mytype_get_sidebar', '' ) );

But it doesn't work. var_dump returns NULL:

$my = get_sidebar ( apply_filters( 'mytype_get_sidebar', '' ) );

Is there any way how to do that?

Thanks in advance.

2 Answers 2


get_sidebar hook in get_sidebar() is action, not a filter. It doesn't expose template (just sidebar name) and doesn't allow to modify data either.

There is no clean way to override internal logic of get_sidebar(), you'll have to approach it in some different way.

  • Yes, I can just do like this: include('sidebar-mytype.php'), but maybe there is another way...
    – Irina
    Mar 20, 2014 at 17:20

I've used load_template to call for sidebar template:

load_template ( dirname( __FILE__ ) . '/sidebar-mytype.php' ) ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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