is_dynamic_sidebar()WP 2.2.0

Whether the dynamic sidebar is enabled and used by theme.

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

No Hooks.

Return

true|false. True if using widgets, false otherwise.

Usage

is_dynamic_sidebar();

Examples

0

#1 Check the presence of widgets

Display the text if the theme supports widgets:

<?php 
if ( is_active_sidebar( 'left-sidebar' ) ) :  ?>
	<ul id="sidebar">
		<?php dynamic_sidebar( 'left-sidebar' ); ?>
	</ul>
<?php endif; ?>

<?php if( is_dynamic_sidebar() ){
	// Theme supports widgets and at least one widget is installed
	// for the widget panel. Let's bring it out:
	?>
	<ul id="sidebar">
		<?php dynamic_sidebar(); ?>
	</ul>
<?php } ?>
0

#2 Checking for a sidebar

To check the presence of sidebars in the plugin code, when you want to connect a widget to a theme, only if there is at least one sidebar, you might need to put a priority on the hook widgets_init, because if you don't put a priority, your function might work faster than at least one sidebar is registered:

add_action( 'widgets_init', 'widget_init_my_widget', 999 );

Notes

  • Global. Array. $wp_registered_widgets The registered widgets.
  • Global. Array. $wp_registered_sidebars The registered sidebars.

Changelog

Since 2.2.0 Introduced.

is_dynamic_sidebar() code WP 6.5.3

function is_dynamic_sidebar() {
	global $wp_registered_widgets, $wp_registered_sidebars;

	$sidebars_widgets = get_option( 'sidebars_widgets' );

	foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
		if ( ! empty( $sidebars_widgets[ $index ] ) ) {
			foreach ( (array) $sidebars_widgets[ $index ] as $widget ) {
				if ( array_key_exists( $widget, $wp_registered_widgets ) ) {
					return true;
				}
			}
		}
	}

	return false;
}