wppaste
WordPress

wp_set_option_autoload_values( array $options ): array

Since
6.4.0, 6.7.0
Source
wp-includes/option.php:397
Sets the autoload values for multiple options in the database.

Description

Autoloading too many options can lead to performance problems, especially if the options are not frequently used.
This function allows modifying the autoload value for multiple options without changing the actual option value.
This is for example recommended for plugin activation and deactivation hooks, to ensure any options exclusively used by the plugin which are generally autoloaded can be set to not autoload when the plugin is inactive.

Compatibility

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

$optionsarray
Associative array of option names and their autoload values to set. The option names are expected to not be SQL-escaped. The autoload values should be boolean values. For backward compatibility 'yes' and 'no' are also accepted, though using these values is deprecated.

Return value

array
Associative array of all provided $options as keys and boolean values for whether their autoload value was updated.

Performance profile

How much work a call to wp_set_option_autoload_values() 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

Reaches the database via ->get_col().

Scaling
Scales with input

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

Instructions
4–51

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
4

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

What it touches

  • cacheobject cachewp_cache_delete_multiple()called directly
  • sqldatabase query->get_col()called directly
  • hookthird-party callbacksapply_filters()one call below wp_set_option_autoload_values()

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

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

WhenInstructionsCalls it makes
always4none
always29–38->prepare(), ->get_col()
$grouped_options43–46->prepare(), ->get_col(), wp_cache_delete_multiple(), wp_cache_delete()
always47–51->prepare(), ->get_col(), wp_load_alloptions(), wp_cache_set()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1634–5124
8.51634–5124
8.41634–51249 fewer instructions than PHP 8.3
8.31724–5424
8.21724–5424
8.11724–5424
7.41724–5424

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

Used by · 4

Source code

function wp_set_option_autoload_values( array $options ) {	global $wpdb; 	if ( ! $options ) {		return array();	} 	$grouped_options = array(		'on'  => array(),		'off' => array(),	);	$results         = array();	foreach ( $options as $option => $autoload ) {		wp_protect_special_option( $option ); // Ensure only valid options can be passed. 		/*		 * Sanitize autoload value and categorize accordingly.		 * The values 'yes', 'no', 'on', and 'off' are supported for backward compatibility.		 */		if ( 'off' === $autoload || 'no' === $autoload || false === $autoload ) {			$grouped_options['off'][] = $option;		} else {			$grouped_options['on'][] = $option;		}		$results[ $option ] = false; // Initialize result value.	} 	$where      = array();	$where_args = array();	foreach ( $grouped_options as $autoload => $options ) {		if ( ! $options ) {			continue;		}		$placeholders = implode( ',', array_fill( 0, count( $options ), '%s' ) );		$where[]      = "autoload != '%s' AND option_name IN ($placeholders)";		$where_args[] = $autoload;		foreach ( $options as $option ) {			$where_args[] = $option;		}	}	$where = 'WHERE ' . implode( ' OR ', $where ); 	/*	 * Determine the relevant options that do not already use the given autoload value.	 * If no options are returned, no need to update.	 */	// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare	$options_to_update = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options $where", $where_args ) );	if ( ! $options_to_update ) {		return $results;	} 	// Run UPDATE queries as needed (maximum 2) to update the relevant options' autoload values to 'yes' or 'no'.	foreach ( $grouped_options as $autoload => $options ) {		if ( ! $options ) {			continue;		}		$options                      = array_intersect( $options, $options_to_update );		$grouped_options[ $autoload ] = $options;		if ( ! $grouped_options[ $autoload ] ) {			continue;		} 		// Run query to update autoload value for all the options where it is needed.		$success = $wpdb->query(			$wpdb->prepare(				"UPDATE $wpdb->options SET autoload = %s WHERE option_name IN (" . implode( ',', array_fill( 0, count( $grouped_options[ $autoload ] ), '%s' ) ) . ')',				array_merge(					array( $autoload ),					$grouped_options[ $autoload ]				)			)		);		if ( ! $success ) {			// Set option list to an empty array to indicate no options were updated.			$grouped_options[ $autoload ] = array();			continue;		} 		// Assume that on success all options were updated, which should be the case given only new values are sent.

Changelog

Introduced in 6.4.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.

6.7.0
The autoload values 'yes' and 'no' are deprecated.from the docblock
6.4.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/option.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.