is_active_sidebar()
Checks if the specified widget panel (area for widgets, sidebar) is used. It will trigger if there is at least one widget in the widget area. Conditional tag.
Checks if the sidebar is active (if any widgets have been added to the specified area).
To check if the theme supports the widget feature and if there is at least one active panel with widgets, use the function is_dynamic_sidebar().
If you need to check if a specific widget is active, rather than the widget panel, use the function is_active_widget()
Hooks from the function
Returns
true|false.
true— at least one widget has been added.false— the sidebar has no widgets.
Usage
if( is_active_sidebar( $index ) ){
// code
}
- $index(string/integer) (required)
- The name of the widget panel, widget area (sidebar), panel ID, or ordinal number. If a number is passed, the function looks for the area with the identifier
sidebar-$index.
Examples
#1 Determine if the widget is active
Display different text, depending on whether the widget is active or not:
if ( is_active_sidebar(1) ) {
echo 'Sidebar 1 has widgets';
}
else {
echo 'Sidebar 1 is empty';
} #2 Just not an empty widget bar
Display the 'left-sidebar' widget bar in the template only if at least one widget is added for it:
<?php if ( is_active_sidebar( 'left-sidebar' ) ){ ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'left-sidebar' ); ?>
</ul>
<?php } ?>
Changelog
| Since 2.8.0 | Introduced. |
is_active_sidebar() is active sidebar code WP 7.0
function is_active_sidebar( $index ) {
$index = ( is_int( $index ) ) ? "sidebar-$index" : sanitize_title( $index );
$sidebars_widgets = wp_get_sidebars_widgets();
$is_active_sidebar = ! empty( $sidebars_widgets[ $index ] );
/**
* Filters whether a dynamic sidebar is considered "active".
*
* @since 3.9.0
*
* @param bool $is_active_sidebar Whether or not the sidebar should be considered "active".
* In other words, whether the sidebar contains any widgets.
* @param int|string $index Index, name, or ID of the dynamic sidebar.
*/
return apply_filters( 'is_active_sidebar', $is_active_sidebar, $index );
}