Make WordPress Core

Opened 15 years ago

Closed 15 years ago

#10956 closed enhancement (wontfix)

replacement is_active_sidebar - wp-includes/widgets.php

Reported by: frumph's profile Frumph Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.9
Component: General Keywords: has-patch
Focuses: Cc:


This uses the new sidebar global and allows for checking via name of the sidebar and the #

 * function is_active_sidebar
 * check if a sidebar has widgets based on index number or name
 * @param $index - sidebar name made with register_sidebar(array('name'=>'Name of Sidebar'), 
 * OR the index # as an int for specific sidebar.
 * @return true if sidebar with $index has widgets, false if not.
function is_active_sidebar( $index ) {
	global $wp_registered_sidebars, $_wp_sidebars_widgets;
	if ( is_int($index) ) {
		if (!empty($_wp_sidebars_widgets[sanitize_title("sidebar-$index")]) )
			return true;
	} else {
		$i = 1;
		foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
			if ( $index == $registered_sidebar['name'] && !empty($_wp_sidebars_widgets[sanitize_title("sidebar-$i")]) )
				return true;
	return false;

Attachments (1)

is_active_sidebar.diff (1001 bytes) - added by Frumph 15 years ago.
is_sidebar_active diff

Download all attachments as: .zip

Change History (15)

#1 @Frumph
15 years ago

Example Use:

<?php if (comicpress_is_active_sidebar('Over Comic')) { ?>
<div id="sidebar-overcomic">
		<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Over Comic') ) : ?><?php endif; ?>
<?php } ?>

#2 @Frumph
15 years ago

Minus the comicpress_ part, I originally made it for our comicpress theme, (whoops) left that in on accident.

#4 @Frumph
15 years ago

Just close this then if you don't want it.

#5 @scribu
15 years ago

It's not about not wanting it. It's about making it easier for people to review and test it.

15 years ago

is_sidebar_active diff

#6 @Frumph
15 years ago

hey waddaya know, I figured out how to do it =) man that wasnt easy, glad I found out for future

#7 @scribu
15 years ago

  • Keywords has-patch added
  • Milestone changed from Unassigned to 2.9

#8 @CharlesClarkson
15 years ago

The patched function fails if a lower numbered sidebar is unregistered (unregister_sidebar()) than the one be searched for. In my test case I tried to test sidebar-5 by the sidebar name (Sidebar Top) after having removed sidebar-2. This version of the function has no way to "know" that an array key was unset and in a 5 sidebar theme (with 1 removed) it never tests for sidebar-5 because $i never gets that high.

register_sidebar(array('name'=>'Sidebar Bottom Left',));
register_sidebar(array('name'=>'Sidebar Bottom Right',));
register_sidebar(array('name'=>'468x60 Header Banner',));
register_sidebar(array('name'=>'468x60 Post Banner',));
register_sidebar(array('name'=>'Sidebar Top',));

This function also fails if the sidebar was registered with a different id from the default:

register_sidebar(array('name'=>'Sidebar Top','id'=>'top-sidebar'));

Without an internal id that cannot be changed by users, checking a sidebar by an integer ( is_active_sidebar(1) ) is not guaranteed to work.

#9 follow-up: @Frumph
15 years ago

Meh, it works for me, of course i'm not unregistering sidebars while the theme is loaded just so I can have the widgets float to whatever sidebar is closest to them if turned on and off.

Very valid points however. Someone should rework this.

#10 in reply to: ↑ 9 @CharlesClarkson
15 years ago

Replying to Frumph:

Someone should rework this.

There is already another solution open for this: #10440 which uses the dynamic_sidebar() code.

#11 @scribu
15 years ago

Cross referencing: #11160

#12 @Denis-de-Bernardy
15 years ago

Big -1 on the current patch. Please see r11562 and r11691.

#13 @Denis-de-Bernardy
15 years ago

Also see #10300 for the reasoning behind r11691.

#14 @ryan
15 years ago

  • Milestone 2.9 deleted
  • Resolution set to wontfix
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.