Using WordPress ‘is_dynamic_sidebar()’ PHP function

The is_dynamic_sidebar() WordPress PHP function determines whether the dynamic sidebar is enabled and used by the theme.

Usage

if ( is_dynamic_sidebar( 'sidebar-1' ) ) {
    // Do something if the sidebar is active
}

Parameters

  • None

More information

See WordPress Developer Resources: is_dynamic_sidebar()

Examples

Display a message if the sidebar is active

This example checks if the dynamic sidebar ‘sidebar-1’ is active and displays a message accordingly.

if ( is_dynamic_sidebar( 'sidebar-1' ) ) {
    echo 'The sidebar is active!';
}

Display custom content if the sidebar is not active

This example shows custom content if the dynamic sidebar ‘sidebar-1’ is not active.

if ( ! is_dynamic_sidebar( 'sidebar-1' ) ) {
    echo 'This is custom content displayed when the sidebar is not active.';
}

Display different content based on the sidebar’s status

This example displays different content based on the dynamic sidebar ‘sidebar-1’ being active or not.

if ( is_dynamic_sidebar( 'sidebar-1' ) ) {
    echo 'The sidebar is active. Display specific content here.';
} else {
    echo 'The sidebar is not active. Display alternative content here.';
}

Count the number of active sidebars

This example counts the number of active sidebars and displays the result.

$active_sidebars = 0;

for ( $i = 1; $i <= 4; $i++ ) {
    if ( is_dynamic_sidebar( 'sidebar-' . $i ) ) {
        $active_sidebars++;
    }
}

echo "There are $active_sidebars active sidebars.";

Display custom content for each active sidebar

This example checks each sidebar in the array for activity and displays custom content for each active sidebar.

$sidebars = array( 'sidebar-1', 'sidebar-2', 'sidebar-3' );

foreach ( $sidebars as $sidebar ) {
    if ( is_dynamic_sidebar( $sidebar ) ) {
        echo "The $sidebar is active. Display content for this sidebar.";
    }
}