The is_active_sidebar() WordPress PHP function determines whether a sidebar contains widgets.
Usage
is_active_sidebar( $index );
Example:
if ( is_active_sidebar( 'left-sidebar' ) ) {
// Display the left-sidebar contents
}
Parameters
- $index (string|int) – Required. Sidebar name, id, or number to check.
More information
See WordPress Developer Resources: is_active_sidebar()
Examples
Display sidebar only if it has widgets
This example will display the sidebar’s content only if it has active widgets.
if ( is_active_sidebar( 'sidebar-1' ) ) {
dynamic_sidebar( 'sidebar-1' );
}
Display a message if the sidebar is empty
This example will display a message if the sidebar doesn’t contain any widgets.
if ( is_active_sidebar( 'sidebar-1' ) ) {
dynamic_sidebar( 'sidebar-1' );
} else {
echo '**No widgets found.**';
}
Display different sidebars based on their activity
This example will display the first active sidebar among ‘left-sidebar’ and ‘right-sidebar’.
if ( is_active_sidebar( 'left-sidebar' ) ) {
dynamic_sidebar( 'left-sidebar' );
} elseif ( is_active_sidebar( 'right-sidebar' ) ) {
dynamic_sidebar( 'right-sidebar' );
}
Display different content based on sidebar activity
This example will display different content depending on whether the ‘footer-sidebar’ is active or not.
if ( is_active_sidebar( 'footer-sidebar' ) ) {
dynamic_sidebar( 'footer-sidebar' );
} else {
echo '**Custom default footer content.**';
}
Display multiple active sidebars
This example will display all active sidebars with the IDs ‘sidebar-1’, ‘sidebar-2’, and ‘sidebar-3’.
for ($i = 1; $i <= 3; $i++) {
if ( is_active_sidebar( "sidebar-$i" ) ) {
dynamic_sidebar( "sidebar-$i" );
}
}