Make WordPress Core

Opened 11 years ago

Last modified 6 years ago

#26636 new enhancement

There Is No Filter For get_sidebar()

Reported by: shelob9's profile shelob9 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 1.5
Component: Themes Keywords: has-patch
Focuses: template Cc:

Description (last modified by SergeyBiryukov)

Because there is no filter for get_sidebar it is not possible to change which sidebar is called conditionally from a plugin and there is no good way to do it in a theme.

Here is an example use for the filter I am proposing to add:

function slug_mobile_sidebar( $name ) {
	if ( wp_is_mobile() ) { 
		$name = 'mobile';

	return $name;
add_filter( 'the_sidebar', 'slug_mobile_sidebar' );

Yes, I could go through a theme and wrap every instance of get_sidebar() in some conditionals or create my own sidebar function. Both of these solutions are inadequate because they require modifying theme files that I might not want to modify. For example, if I was creating a child theme, I would have to add every template to it for either of these solutions to work. They also make my code much less portable.

Adding a filter to get_sidebar would enable plugin developers to write similar functions to add mobile sidebars, to stick with the same example. If implemented from a plugin that function would of course require a template redirect so WordPress could load sidebar-mobile.php from the plugin if it didn't exist in the theme directory.

Attachments (1)

26636.diff (612 bytes) - added by shelob9 11 years ago.
Adds a filter to get_sidebar

Download all attachments as: .zip

Change History (12)

11 years ago

Adds a filter to get_sidebar

#1 @shelob9
11 years ago

  • Cc JPollock412@… added
  • Keywords has-patch added

#2 @SergeyBiryukov
11 years ago

  • Component changed from General to Template
  • Description modified (diff)

Related: #13239, #14310, #21062.

#3 @johnbillion
11 years ago

Dupe of #20287? (just deals with the header and footer but could add support for the sidebar too.)

#4 @SergeyBiryukov
11 years ago

I don't see any filters suggested in #20287, so this looks more like a duplicate of #13239 and the related tickets to me (with a more specific use case).

#5 @shelob9
11 years ago

@SergeyBiryukov I think @nacin made a really great point about #13239 " We may be able to add a specific, targeted hook, or introduce a function or argument that exposes filters. But, I do not like the idea of "generic" filters that will run over and over again," You and @johnbillion have referenced a bunch of attempts to add these sorts of non-specific filters. The patches and tickets are very helpful for me to look at, so I thank you for bring them to my attention.

That said, I think creating a simple, specific filter, just to target sidebars makes more sense. This filter would have several uses beyond just creating a way to change widget areas, or wrap the sidebar in a specific container so a jQuery plugin could act on it. I could imagine changing out entire sidebar areas based on if a user is a paid member of a site or has registered for free. Also what about filtering which sidebar is used based on information from the cookie, like time since last login. Another idea would be to filter sidebar based on user role. For example, what about a sidebar that showed analytics info for the current post and the site, as well as editing links that was shown only to admins, while other users got a sidebar intended to be forward facing? That's just a few of the cool ideas for plugins that this filter would make possible.

#6 @nacin
10 years ago

  • Component changed from Template to Themes
  • Focuses template added

#7 @dbaker
10 years ago

+1 for being able to filter get_header() and get_footer(). It would be very useful when doing partial page loading for things like PJAX or pushState history type work.

Currently it requires every template file to have an if statement around get_header() and get_footer().

#8 @justnorris
9 years ago

*beep beep beep beep*

Is this coming to wordpress some day ?

I'd actually love to see this in a combination with get_header() being filtered too, that way the theme ( or a plugin ) could decide whether to output the header and sidebar. As @dbaker said - for PJAX/pushState

My current workaround is to use a "maybe_get_sidebar($sidebar)" function throughout my theme, but this would be a lot more elegant :)

#9 @Shelob9
9 years ago


I think, and I could be wrong, and I definitely do not speak for anyone with authority, but the opposition is to introducing several filters, 'get_sidebar', 'get_header', 'get_footer', when each of the functions they will be in actually use locate_template(). Therefore, even though it would be less intuitive to use, we should add one filter in locate_template().

Then you could do something like:

add_filter( 'locate_template', function( $template ) {
if ( 'sidebar.php' == $template ) {
	$template = 'something.php';
return $template;

That's harder to use and would require more conditional logic, especially if you have multiple sidebars, but ti would work and now we only have one new filter, instead of three.

I'd write that patch, but I know there is a long discussion in another ticket for refactoring locate_template(), so that would be a blocker. Also that discussion may result in a filter being added to locate_template() and then we don't exactly need a get_sidebar filter.

#10 in reply to: ↑ description @wedgetech
7 years ago

Is this still a thing? It would be great to have this functionality in the core.

This ticket was mentioned in Slack in #themereview by joyously. View the logs.

6 years ago

Note: See TracTickets for help on using tickets.