is_registered_sidebar()WP 4.4.0

Checks if the specified widget area is registered.

Helps to work safely with sidebars without causing errors. This check is convenient to use before calling dynamic_sidebar() or when migrating/updating a theme, when the set of sidebars may change.

If the area is not found, a fallback block can be displayed or the code can be skipped.

The function simply checks if the key exists in the global array $wp_registered_sidebars, so it works very quickly.

The sidebar identifier is case-sensitive and must match the id passed to register_sidebar().

1 time — 0.000013 sec (very fast) | 50000 times — 0.02 sec (speed of light)

No Hooks.

Returns

true|false.

  • true — a sidebar with such id is registered.
  • false — otherwise.

Usage

is_registered_sidebar( $sidebar_id );
$sidebar_id(string/int) (required)
ID of the widget area that was specified in the id parameter when registering the widget using register_sidebar().

Examples

0

#1 Check if the 'sidebar' widget panel is already there and register it if not.

add_action( 'widgets_init', 'register_my_widgets' );

function register_my_widgets(){

	if( is_registered_sidebar( 'sidebar' ) ){
		return;
	}

	register_sidebar( [
		'name'         => 'website sidebar',
		'id'           => 'sidebar',
		'description'  => 'These widgets will be shown in the sidebar of the site',
		'before_title' => '<h1>',
		'after_title'  => '</h1>'
	] );
}

Notes

  • Global. Array. $wp_registered_sidebars The registered sidebars.

Changelog

Since 4.4.0 Introduced.

is_registered_sidebar() code WP 6.9

function is_registered_sidebar( $sidebar_id ) {
	global $wp_registered_sidebars;

	return isset( $wp_registered_sidebars[ $sidebar_id ] );
}