wppaste
WordPress

wp_convert_widget_settings( string $base_name, string $option_name, array $settings ): array

Since
2.8.0
Source
wp-includes/widgets.php:1139
Converts the widget settings from single to multi-widget format.

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

$base_namestring
Root ID for all widgets of this type.
$option_namestring
Option name for this widget type.
$settingsarray
The array of widget instance settings.

Return value

array
The array of widget settings converted to multi-widget format.

Performance profile

How much work a call to wp_convert_widget_settings() 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
16–71

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()one call below wp_convert_widget_settings()
  • cacheobject cachewp_cache_get()one call below wp_convert_widget_settings()
  • serializeserialisationmaybe_unserialize()one call below wp_convert_widget_settings()

Further down the call graph this can also reach 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 · 20 distinct outcomes

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

WhenInstructionsCalls it makes
empty($settings) && !is_admin()16is_admin()
!empty($settings) && !is_admin()19–25array_keys(), is_admin()
empty($settings) && is_admin()20is_admin(), update_option()
!empty($settings) && is_admin()23–29array_keys(), is_admin(), update_option()
empty($settings) && !is_admin() && !empty($value)31–48is_admin(), is_admin(), is_admin()
empty($settings) && !is_admin()32–54is_admin(), get_option(), is_admin(), is_admin()
!empty($settings) && !is_admin() && !empty($value)34–57array_keys(), is_admin(), is_admin(), is_admin()
empty($settings) && !empty($value)35–52is_admin(), is_admin(), is_admin(), update_option()
!empty($settings) && !is_admin()35–63array_keys(), is_admin(), get_option(), is_admin(), is_admin()
empty($settings) && !empty($value)36–52is_admin(), is_admin(), update_option(), is_admin()
empty($settings) && is_admin()36–58is_admin(), get_option(), is_admin(), is_admin(), update_option()
empty($settings)37–58is_admin(), get_option(), is_admin(), update_option(), is_admin()
8 further outcomes, up to 71 instructions
!empty($settings) && !empty($value)38–61array_keys(), is_admin(), is_admin(), is_admin(), update_option()
!empty($settings) && !empty($value)39–61array_keys(), is_admin(), is_admin(), update_option(), is_admin()
!empty($settings) && is_admin()39–67array_keys(), is_admin(), get_option(), is_admin(), is_admin(), update_option()
empty($settings) && !empty($value)40–56is_admin(), is_admin(), update_option(), is_admin(), update_option()
!empty($settings)40–67array_keys(), is_admin(), get_option(), is_admin(), update_option(), is_admin()
empty($settings) && is_admin()41–62is_admin(), get_option(), is_admin(), update_option(), is_admin(), update_option()
!empty($settings) && !empty($value)43–65array_keys(), is_admin(), is_admin(), update_option(), is_admin(), update_option()
!empty($settings) && is_admin()44–71array_keys(), is_admin(), get_option(), is_admin(), update_option(), is_admin(), update_option()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8016–7117
8.58016–7117
8.48016–71172 fewer instructions than PHP 8.3
8.38216–7317
8.28216–7317
8.18216–73173 fewer instructions than PHP 7.4
7.48516–7617

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 · 3

  • is_admin()Determines whether the current request is for an administrative interface page.
  • get_option()Retrieves an option value based on an option name.
  • update_option()Updates the value of an option that was already added.

Used by · 1

Source code

function wp_convert_widget_settings( $base_name, $option_name, $settings ) {	// This test may need expanding.	$single  = false;	$changed = false; 	if ( empty( $settings ) ) {		$single = true;	} else {		foreach ( array_keys( $settings ) as $number ) {			if ( 'number' === $number ) {				continue;			}			if ( ! is_numeric( $number ) ) {				$single = true;				break;			}		}	} 	if ( $single ) {		$settings = array( 2 => $settings ); 		// If loading from the front page, update sidebar in memory but don't save to options.		if ( is_admin() ) {			$sidebars_widgets = get_option( 'sidebars_widgets' );		} else {			if ( empty( $GLOBALS['_wp_sidebars_widgets'] ) ) {				$GLOBALS['_wp_sidebars_widgets'] = get_option( 'sidebars_widgets', array() );			}			$sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets'];		} 		foreach ( (array) $sidebars_widgets as $index => $sidebar ) {			if ( is_array( $sidebar ) ) {				foreach ( $sidebar as $i => $name ) {					if ( $base_name === $name ) {						$sidebars_widgets[ $index ][ $i ] = "$name-2";						$changed                          = true;						break 2;					}				}			}		} 		if ( is_admin() && $changed ) {			update_option( 'sidebars_widgets', $sidebars_widgets );		}	} 	$settings['_multiwidget'] = 1;	if ( is_admin() ) {		update_option( $option_name, $settings );	} 	return $settings;}

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.