Using WordPress ‘is_registered_sidebar()’ PHP function

The is_registered_sidebar() WordPress PHP function checks if a sidebar is registered.

Usage

is_registered_sidebar( $sidebar_id );

Example:

Input:

is_registered_sidebar( 'custom-sidebar' );

Output:

true // If the sidebar is registered

Parameters

  • $sidebar_id (string|int) (Required) – The ID of the sidebar when it was registered.

More information

See WordPress Developer Resources: is_registered_sidebar()

Examples

Check if a specific sidebar is registered

Check if the ‘custom-sidebar’ is registered and display a message accordingly.

if ( is_registered_sidebar( 'custom-sidebar' ) ) {
    echo 'Custom sidebar is registered.';
} else {
    echo 'Custom sidebar is not registered.';
}

Display a fallback sidebar

Show a fallback sidebar when the preferred sidebar is not registered.

$preferred_sidebar = 'preferred-sidebar';
$fallback_sidebar = 'fallback-sidebar';

if ( is_registered_sidebar( $preferred_sidebar ) ) {
    dynamic_sidebar( $preferred_sidebar );
} else {
    dynamic_sidebar( $fallback_sidebar );
}

Register a sidebar only if not already registered

Register a new sidebar with the ID ‘new-sidebar’ only if it’s not already registered.

$new_sidebar_id = 'new-sidebar';

if ( ! is_registered_sidebar( $new_sidebar_id ) ) {
    register_sidebar( array(
        'id' => $new_sidebar_id,
        'name' => 'New Sidebar',
    ) );
}

Unregister a sidebar if it’s registered

Unregister a sidebar with the ID ‘unwanted-sidebar’ if it’s registered.

$unwanted_sidebar_id = 'unwanted-sidebar';

if ( is_registered_sidebar( $unwanted_sidebar_id ) ) {
    unregister_sidebar( $unwanted_sidebar_id );
}

Display a message for each registered sidebar

Loop through an array of sidebar IDs and display a message for each registered sidebar.

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

foreach ( $sidebars as $sidebar_id ) {
    if ( is_registered_sidebar( $sidebar_id ) ) {
        echo "The sidebar '{$sidebar_id}' is registered.";
    }
}