wppaste
WordPress

retrieve_widgets( string|bool $theme_changed = false ): array

Since
2.8.0
Source
wp-includes/widgets.php:1325
Validates and remaps any "orphaned" widgets to wp_inactive_widgets sidebar, and saves the widget settings. This has to run at least on each theme change.

Description

For example, let's say theme A has a "footer" sidebar, and theme B doesn't have one.
After switching from theme A to theme B, all the widgets previously assigned to the footer would be inaccessible. This function detects this scenario, and moves all the widgets previously assigned to the footer under wp_inactive_widgets.

Despite the word "retrieve" in the name, this function actually updates the database and the global $sidebars_widgets. For that reason it should not be run on front end, unless the $theme_changed value is 'customize' (to bypass the database write).

Compatibility

WordPress
since 2.8.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

$theme_changedstring|booloptional
Whether the theme was changed as a boolean. A value of 'customize' defers updates for the Customizer.Default: false

Return value

array
Updated sidebars widgets.

Performance profile

How much work a call to retrieve_widgets() 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 update_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
20–74

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

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

What it touches

  • hookthird-party callbacksapply_filters()one call below retrieve_widgets()
  • optionoption read or writeupdate_option()one call below retrieve_widgets()

Further down the call graph this can also reach serialize, cache, transient and query. 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 retrieve_widgets() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($sidebars_widgets)20array_keys(), array_keys(), get_theme_mod()
!empty($sidebars_widgets) && $sidebars_widgets_keys === false38array_keys(), array_keys(), get_theme_mod(), array_keys(), sort(), sort(), _wp_remove_unregistered_widgets()
$theme_changed === "customize"54–56array_keys(), array_keys(), get_theme_mod(), _wp_remove_unregistered_widgets(), wp_map_sidebars_widgets(), array_values(), array_merge(), array_diff(), array_merge()
$theme_changed !== "customize"57–59array_keys(), array_keys(), get_theme_mod(), _wp_remove_unregistered_widgets(), wp_map_sidebars_widgets(), array_values(), array_merge(), array_diff(), array_merge(), wp_set_sidebars_widgets()
!empty($sidebars_widgets) && $sidebars_widgets_keys !== false && $theme_changed === "customize"69–71array_keys(), array_keys(), get_theme_mod(), array_keys(), sort(), sort(), _wp_remove_unregistered_widgets(), wp_map_sidebars_widgets(), array_values(), array_merge(), array_diff(), array_merge()
!empty($sidebars_widgets) && $sidebars_widgets_keys !== false && $theme_changed !== "customize"72–74array_keys(), array_keys(), get_theme_mod(), array_keys(), sort(), sort(), _wp_remove_unregistered_widgets(), wp_map_sidebars_widgets(), array_values(), array_merge(), array_diff(), array_merge(), wp_set_sidebars_widgets()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9820–7411
8.59820–7411
8.49820–74115 fewer instructions than PHP 8.3
8.310320–7411
8.210320–7411
8.110320–74111 more instruction than PHP 7.4
7.410220–7311

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.

Uses · 4

Used by · 5

Source code

function retrieve_widgets( $theme_changed = false ) {	global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets; 	$registered_sidebars_keys = array_keys( $wp_registered_sidebars );	$registered_widgets_ids   = array_keys( $wp_registered_widgets ); 	if ( ! is_array( get_theme_mod( 'sidebars_widgets' ) ) ) {		if ( empty( $sidebars_widgets ) ) {			return array();		} 		unset( $sidebars_widgets['array_version'] ); 		$sidebars_widgets_keys = array_keys( $sidebars_widgets );		sort( $sidebars_widgets_keys );		sort( $registered_sidebars_keys ); 		if ( $sidebars_widgets_keys === $registered_sidebars_keys ) {			$sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids ); 			return $sidebars_widgets;		}	} 	// Discard invalid, theme-specific widgets from sidebars.	$sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids );	$sidebars_widgets = wp_map_sidebars_widgets( $sidebars_widgets ); 	// Replace non-array values inside the array with an empty array.	foreach ( $sidebars_widgets as $key => $value ) {		if ( ! is_array( $value ) ) {			$sidebars_widgets[ $key ] = array();		}	} 	// Find hidden/lost multi-widget instances.	$shown_widgets = array_merge( ...array_values( $sidebars_widgets ) );	$lost_widgets  = array_diff( $registered_widgets_ids, $shown_widgets ); 	foreach ( $lost_widgets as $key => $widget_id ) {		$number = preg_replace( '/.+?-([0-9]+)$/', '$1', $widget_id ); 		// Only keep active and default widgets.		if ( is_numeric( $number ) && (int) $number < 2 ) {			unset( $lost_widgets[ $key ] );		}	}	$sidebars_widgets['wp_inactive_widgets'] = array_merge( $lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets'] ); 	if ( 'customize' !== $theme_changed ) {		// Update the widgets settings in the database.		wp_set_sidebars_widgets( $sidebars_widgets );	} 	return $sidebars_widgets;}

Changelog

Introduced in 2.8.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.9.7 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.