WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

#10956 closed enhancement (wontfix)

replacement is_active_sidebar - wp-includes/widgets.php

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

Description

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;
			$i++; 
		}
	}
	return false;
}

Attachments (1)

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

Download all attachments as: .zip

Change History (15)

comment:1 Frumph5 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; ?>
</div>
<?php } ?>

comment:2 Frumph5 years ago

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

comment:4 Frumph5 years ago

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

comment:5 scribu5 years ago

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

Frumph5 years ago

is_sidebar_active diff

comment:6 Frumph5 years ago

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

comment:7 scribu5 years ago

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

comment:8 CharlesClarkson4 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',));
unregister_sidebar('sidebar-2');

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.

comment:9 follow-up: Frumph4 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.

comment:10 in reply to: ↑ 9 CharlesClarkson4 years ago

Replying to Frumph:

Someone should rework this.

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

comment:11 scribu4 years ago

Cross referencing: #11160

comment:12 Denis-de-Bernardy4 years ago

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

comment:13 Denis-de-Bernardy4 years ago

Also see #10300 for the reasoning behind r11691.

comment:14 ryan4 years ago

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