Opened 17 years ago
Closed 16 years ago
#10956 closed enhancement (wontfix)
replacement is_active_sidebar - wp-includes/widgets.php
| Reported by: |
|
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)
Change History (15)
#2
@
17 years ago
Minus the comicpress_ part, I originally made it for our comicpress theme, (whoops) left that in on accident.
#3
@
17 years ago
Please submit your code in diff form.
See http://markjaquith.wordpress.com/2005/11/02/my-wordpress-toolbox/
#5
@
17 years ago
It's not about not wanting it. It's about making it easier for people to review and test it.
#6
@
17 years ago
hey waddaya know, I figured out how to do it =) man that wasnt easy, glad I found out for future
#8
@
17 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.
#9
follow-up:
↓ 10
@
17 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.
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 } ?>