wppaste
WordPress

dynamic_sidebar( int|string $index = 1 ): bool

Since
2.2.0
Source
wp-includes/widgets.php:697
Displays dynamic sidebar.

Description

By default this displays the default sidebar or 'sidebar-1'. If your theme specifies the 'id' or 'name' parameter for its registered sidebars you can pass an ID or name as the $index parameter.
Otherwise, you can pass in a numerical index to display the sidebar at that index.

Compatibility

WordPress
since 2.2.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$indexint|stringoptional
Index, name or ID of dynamic sidebar. Default 1.Default: 1

Return value

bool
True, if widget sidebar was found and called. False if not found or not called.

Performance profile

How much work a call to dynamic_sidebar() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
28–79

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 175.

Plugin surface
5 hooks

Third-party callbacks on 'dynamic_sidebar_before', 'dynamic_sidebar_after', 'dynamic_sidebar_has_widgets' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()called directly
  • optionoption read or writeget_option()one call below dynamic_sidebar()

Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost dynamic_sidebar() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_int($index)28–33wp_get_sidebars_widgets(), do_action(), do_action(), apply_filters()
!is_int($index)33–39sanitize_title(), wp_get_sidebars_widgets(), do_action(), do_action(), apply_filters()
!is_int($index) && !$wp_registered_sidebars && $index === false43–48sanitize_title(), sanitize_title(), wp_get_sidebars_widgets(), do_action(), do_action(), apply_filters()
is_int($index) && !empty($wp_registered_sidebars[$index]) && !empty($sidebars_widgets[$index])55–64wp_get_sidebars_widgets(), sprintf(), do_action(), is_admin(), is_admin(), do_action(), apply_filters()
!is_int($index) && !empty($wp_registered_sidebars[$index]) && !empty($sidebars_widgets[$index])60–70sanitize_title(), wp_get_sidebars_widgets(), sprintf(), do_action(), is_admin(), is_admin(), do_action(), apply_filters()
!is_int($index) && !$wp_registered_sidebars && $index === false && !empty($wp_registered_sidebars[$index]) && !empty($sidebars_widgets[$index])70–79sanitize_title(), sanitize_title(), wp_get_sidebars_widgets(), sprintf(), do_action(), is_admin(), is_admin(), do_action(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev17528–7919
8.517528–7919
8.417528–79193 fewer instructions than PHP 8.3
8.317828–7919
8.217828–7919
8.117828–79191 more instruction than PHP 7.4
7.417728–7919

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 8

8 hooks fire while dynamic_sidebar() runs, in this order:

  1. do_action( dynamic_sidebar_before )actionline 715 (+18 into the body)

    Fires before widgets are rendered in a dynamic sidebar.

  2. do_action( dynamic_sidebar_after )actionline 717 (+20 into the body)

    Fires after widgets are rendered in a dynamic sidebar.

  3. apply_filters( dynamic_sidebar_has_widgets )filterline 719 (+22 into the body)

    Filters whether a sidebar has widgets.

  4. do_action( dynamic_sidebar_before )actionline 738 (+41 into the body)

    Fires before widgets are rendered in a dynamic sidebar.

  5. apply_filters( dynamic_sidebar_params )filterline 813 (+116 into the body)

    Filters the parameters passed to a widget's display callback.

  6. do_action( dynamic_sidebar )actionline 842 (+145 into the body)

    Fires before a widget's display callback is called.

  7. do_action( dynamic_sidebar_after )actionline 866 (+169 into the body)

    Fires after widgets are rendered in a dynamic sidebar.

  8. apply_filters( dynamic_sidebar_has_widgets )filterline 880 (+183 into the body)

    Filters whether a sidebar has widgets.

Uses · 5

  • sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
  • wp_get_sidebars_widgets()Retrieves the full list of sidebars and their widget instance IDs.
  • do_action()Calls the callback functions that have been added to an action hook.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • is_admin()Determines whether the current request is for an administrative interface page.

Used by · 2

Source code

function dynamic_sidebar( $index = 1 ) {	global $wp_registered_sidebars, $wp_registered_widgets; 	if ( is_int( $index ) ) {		$index = "sidebar-$index";	} else {		$index = sanitize_title( $index );		foreach ( (array) $wp_registered_sidebars as $key => $value ) {			if ( sanitize_title( $value['name'] ) === $index ) {				$index = $key;				break;			}		}	} 	$sidebars_widgets = wp_get_sidebars_widgets();	if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) {		/** This action is documented in wp-includes/widget.php */		do_action( 'dynamic_sidebar_before', $index, false );		/** This action is documented in wp-includes/widget.php */		do_action( 'dynamic_sidebar_after', $index, false );		/** This filter is documented in wp-includes/widget.php */		return apply_filters( 'dynamic_sidebar_has_widgets', false, $index );	} 	$sidebar = $wp_registered_sidebars[ $index ]; 	$sidebar['before_sidebar'] = sprintf( $sidebar['before_sidebar'], $sidebar['id'], $sidebar['class'] ); 	/**	 * Fires before widgets are rendered in a dynamic sidebar.	 *	 * Note: The action also fires for empty sidebars, and on both the front end	 * and back end, including the Inactive Widgets sidebar on the Widgets screen.	 *	 * @since 3.9.0	 *	 * @param int|string $index       Index, name, or ID of the dynamic sidebar.	 * @param bool       $has_widgets Whether the sidebar is populated with widgets.	 *                                Default true.	 */	do_action( 'dynamic_sidebar_before', $index, true ); 	if ( ! is_admin() && ! empty( $sidebar['before_sidebar'] ) ) {		echo $sidebar['before_sidebar'];	} 	$did_one = false;	foreach ( (array) $sidebars_widgets[ $index ] as $id ) { 		if ( ! isset( $wp_registered_widgets[ $id ] ) ) {			continue;		} 		$params = array_merge(			array(				array_merge(					$sidebar,					array(						'widget_id'   => $id,						'widget_name' => $wp_registered_widgets[ $id ]['name'],					)				),			),			(array) $wp_registered_widgets[ $id ]['params']		); 		// Substitute HTML `id` and `class` attributes into `before_widget`.		$classname_ = '';		foreach ( (array) $wp_registered_widgets[ $id ]['classname'] as $cn ) {			if ( is_string( $cn ) ) {				$classname_ .= '_' . $cn;			} elseif ( is_object( $cn ) ) {				$classname_ .= '_' . get_class( $cn );			}		}		$classname_ = ltrim( $classname_, '_' ); 		$params[0]['before_widget'] = sprintf(			$params[0]['before_widget'],

Changelog

Introduced in 2.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/widgets.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.