wp_get_sidebar()WP 5.9.0

Retrieves the registered sidebar with the given ID.

No Hooks.

Return

Array|null. The discovered sidebar, or null if it is not registered.

Usage

wp_get_sidebar( $id );
$id(string) (required)
The sidebar ID.

Examples

0

#1 Demo

Suppose we registered a sidebar with function register_sidebar() and we have a sidebar with ID 'sidebar'. Let's get its data:

$sidebar = wp_get_sidebar( 'sidebar' );

print_r( $sidebar );

/*
Array
(
	[name] => Sidebar
	[id] => sidebar
	[description] => Widget block in the sidebar.
	[class] => some-class
	[before_widget] => "</div>\n",
	[after_widget] =>
	[before_title] => '<div class="sb_title">',
	[after_title] => '</div><div class="sb-block">',
	[before_sidebar] =>
	[after_sidebar] =>
	[show_in_rest] =>
)
*/

Notes

  • Global. Array. $wp_registered_sidebars The registered sidebars.

Changelog

Since 5.9.0 Introduced.

wp_get_sidebar() code WP 6.5.3

function wp_get_sidebar( $id ) {
	global $wp_registered_sidebars;

	foreach ( (array) $wp_registered_sidebars as $sidebar ) {
		if ( $sidebar['id'] === $id ) {
			return $sidebar;
		}
	}

	if ( 'wp_inactive_widgets' === $id ) {
		return array(
			'id'   => 'wp_inactive_widgets',
			'name' => __( 'Inactive widgets' ),
		);
	}

	return null;
}