Title: wp_set_option_autoload_values
Published: November 8, 2023
Last modified: April 28, 2025

---

# wp_set_option_autoload_values( array $options ): array

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#wp--skip-link--target)

Sets the autoload values for multiple options in the database.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#parameters)󠁿

 `$options`arrayrequired

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](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#return)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#source)󠁿

    ```php
    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.
    		foreach ( $grouped_options[ $autoload ] as $option ) {
    			$results[ $option ] = true;
    		}
    	}

    	/*
    	 * If any options were changed to 'on', delete their individual caches, and delete 'alloptions' cache so that it
    	 * is refreshed as needed.
    	 * If no options were changed to 'on' but any options were changed to 'no', delete them from the 'alloptions'
    	 * cache. This is not necessary when options were changed to 'on', since in that situation the entire cache is
    	 * deleted anyway.
    	 */
    	if ( $grouped_options['on'] ) {
    		wp_cache_delete_multiple( $grouped_options['on'], 'options' );
    		wp_cache_delete( 'alloptions', 'options' );
    	} elseif ( $grouped_options['off'] ) {
    		$alloptions = wp_load_alloptions( true );

    		foreach ( $grouped_options['off'] as $option ) {
    			if ( isset( $alloptions[ $option ] ) ) {
    				unset( $alloptions[ $option ] );
    			}
    		}

    		wp_cache_set( 'alloptions', $alloptions, 'options' );
    	}

    	return $results;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/option.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/option.php#L397)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/option.php#L397-L505)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_cache_delete_multiple()](https://developer.wordpress.org/reference/functions/wp_cache_delete_multiple/)`wp-includes/cache.php` |

Deletes multiple values from the cache in one call.

  | 
| [wp_cache_delete()](https://developer.wordpress.org/reference/functions/wp_cache_delete/)`wp-includes/cache.php` |

Removes the cache contents matching key and group.

  | 
| [wp_cache_set()](https://developer.wordpress.org/reference/functions/wp_cache_set/)`wp-includes/cache.php` |

Saves the data to the cache.

  | 
| [wp_load_alloptions()](https://developer.wordpress.org/reference/functions/wp_load_alloptions/)`wp-includes/option.php` |

Loads and caches all autoloaded options, if available or all options.

  | 
| [wp_protect_special_option()](https://developer.wordpress.org/reference/functions/wp_protect_special_option/)`wp-includes/option.php` |

Protects WordPress special option from being modified.

  | 
| [wpdb::get_col()](https://developer.wordpress.org/reference/classes/wpdb/get_col/)`wp-includes/class-wpdb.php` |

Retrieves one column from the database.

  | 
| [wpdb::query()](https://developer.wordpress.org/reference/classes/wpdb/query/)`wp-includes/class-wpdb.php` |

Performs a database query, using current database connection.

  | 
| [wpdb::prepare()](https://developer.wordpress.org/reference/classes/wpdb/prepare/)`wp-includes/class-wpdb.php` |

Prepares a SQL query for safe execution.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#)

| Used by | Description | 
| [wp_set_options_autoload()](https://developer.wordpress.org/reference/functions/wp_set_options_autoload/)`wp-includes/option.php` |

Sets the autoload value for multiple options in the database.

  | 
| [wp_set_option_autoload()](https://developer.wordpress.org/reference/functions/wp_set_option_autoload/)`wp-includes/option.php` |

Sets the autoload value for an option in the database.

  | 
| [switch_theme()](https://developer.wordpress.org/reference/functions/switch_theme/)`wp-includes/theme.php` |

Switches the theme.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_set_option_autoload_values/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.7.0](https://developer.wordpress.org/reference/since/6.7.0/) | The autoload values `'yes'` and `'no'` are deprecated. | 
| [6.4.0](https://developer.wordpress.org/reference/since/6.4.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_set_option_autoload_values%2F)
before being able to contribute a note or feedback.