wp_set_option_autoload_values( array $options ): array
- Since
- 6.4.0, 6.7.0
- Source
wp-includes/option.php:397
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
- Scaling
- Scales with input
- Instructions
- 4–51
- Plugin surface
- None
- Called by
- 4
Reaches the database via ->get_col().
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 163.
Nothing here hands control to plugin code.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_delete_multiple()called directly - sqldatabase query
->get_col()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 4 | none |
| always | 29–38 | ->prepare(), ->get_col() |
$grouped_options | 43–46 | ->prepare(), ->get_col(), wp_cache_delete_multiple(), wp_cache_delete() |
| always | 47–51 | ->prepare(), ->get_col(), wp_load_alloptions(), wp_cache_set() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 163 | 4–51 | 24 | |
| 8.5 | 163 | 4–51 | 24 | |
| 8.4 | 163 | 4–51 | 24 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 172 | 4–54 | 24 | |
| 8.2 | 172 | 4–54 | 24 | |
| 8.1 | 172 | 4–54 | 24 | |
| 7.4 | 172 | 4–54 | 24 |
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
- wp_protect_special_option()Protects WordPress special option from being modified.
- wp_cache_delete_multiple()Deletes multiple values from the cache in one call.
- wp_cache_delete()Removes the cache contents matching key and group.
- wp_load_alloptions()Loads and caches all autoloaded options, if available or all options.
- wp_cache_set()Saves the data to the cache.
Used by · 4
- switch_theme()Switches the theme.
- upgrade_650()Executes changes made in WordPress 6.5.0.
- wp_set_option_autoload()Sets the autoload value for an option in the database.
- wp_set_options_autoload()Sets the autoload value for multiple options in the database.
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.
Signature, return type and hooks compared across 5 parsed releases.
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.