retrieve_widgets( string|bool $theme_changed = false ): array
- Since
- 2.8.0
- Source
wp-includes/widgets.php:1325
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
- Scaling
- Scales with input
- Instructions
- 20–74
- Plugin surface
- None
- Called by
- 5
Reads stored settings via update_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 98.
Nothing here hands control to plugin code.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below retrieve_widgets() - optionoption read or write
update_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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($sidebars_widgets) | 20 | array_keys(), array_keys(), get_theme_mod() |
!empty($sidebars_widgets) && $sidebars_widgets_keys === false | 38 | array_keys(), array_keys(), get_theme_mod(), array_keys(), sort(), sort(), _wp_remove_unregistered_widgets() |
$theme_changed === "customize" | 54–56 | array_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–59 | array_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–71 | array_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–74 | array_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 98 | 20–74 | 11 | |
| 8.5 | 98 | 20–74 | 11 | |
| 8.4 | 98 | 20–74 | 11 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 103 | 20–74 | 11 | |
| 8.2 | 103 | 20–74 | 11 | |
| 8.1 | 103 | 20–74 | 11 | 1 more instruction than PHP 7.4 |
| 7.4 | 102 | 20–73 | 11 |
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
- get_theme_mod()Retrieves theme modification value for the active theme.
- _wp_remove_unregistered_widgets()Compares a list of sidebars with their widgets against an allowed list.
- wp_map_sidebars_widgets()Compares a list of sidebars with their widgets against an allowed list.
- wp_set_sidebars_widgets()Sets the sidebar widget option to update sidebars.
Used by · 5
- WP_Customize_Widgets::override_sidebars_widgets_for_theme_switch()Override sidebars_widgets for theme switch.
- WP_REST_Sidebars_Controller::retrieve_widgets()Looks for "lost" widgets once per request.
- WP_REST_Widgets_Controller::retrieve_widgets()Looks for "lost" widgets once per request.
- _wp_sidebars_changed()Handles sidebars config after theme change.
- upgrade_330()Execute changes made in WordPress 3.3.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.