wp_cache_set( 'alloptions', $alloptions, 'options' );
* Sets the autoload value for multiple options in the database.
* This is a wrapper for {@see wp_set_option_autoload_values()}, which can be used to set different autoload values for
* @since 6.7.0 The autoload values 'yes' and 'no' are deprecated.
* @see wp_set_option_autoload_values()
* @param string[] $options List of option names. Expected to not be SQL-escaped.
* @param bool $autoload Autoload value to control whether to load the options when WordPress starts up.
* For backward compatibility 'yes' and 'no' are also accepted, though using these values is
* @return array Associative array of all provided $options as keys and boolean values for whether their autoload value
function wp_set_options_autoload( array $options, $autoload ) {
return wp_set_option_autoload_values(
array_fill_keys( $options, $autoload )
* Sets the autoload value for an option in the database.
* This is a wrapper for {@see wp_set_option_autoload_values()}, which can be used to set the autoload value for
* multiple options at once.
* @since 6.7.0 The autoload values 'yes' and 'no' are deprecated.
* @see wp_set_option_autoload_values()
* @param string $option Name of the option. Expected to not be SQL-escaped.
* @param bool $autoload Autoload value to control whether to load the option when WordPress starts up.
* For backward compatibility 'yes' and 'no' are also accepted, though using these values is
* @return bool True if the autoload value was modified, false otherwise.
function wp_set_option_autoload( $option, $autoload ) {
$result = wp_set_option_autoload_values( array( $option => $autoload ) );
if ( isset( $result[ $option ] ) ) {
return $result[ $option ];
* Protects WordPress special option from being modified.
* Will die if $option is in protected list. Protected options are 'alloptions'
* and 'notoptions' options.
* @param string $option Option name.
function wp_protect_special_option( $option ) {
if ( 'alloptions' === $option || 'notoptions' === $option ) {
/* translators: %s: Option name. */
__( '%s is a protected WP option and may not be modified' ),
* Prints option value after sanitizing for forms.
* @param string $option Option name.
function form_option( $option ) {
echo esc_attr( get_option( $option ) );
* Loads and caches all autoloaded options, if available or all options.
* @since 5.3.1 The `$force_cache` parameter was added.
* @global wpdb $wpdb WordPress database abstraction object.
* @param bool $force_cache Optional. Whether to force an update of the local cache
* from the persistent cache. Default false.
* @return array List of all options.
function wp_load_alloptions( $force_cache = false ) {
* Filters the array of alloptions before it is populated.
* Returning an array from the filter will effectively short circuit
* wp_load_alloptions(), returning that value instead.
* @param array|null $alloptions An array of alloptions. Default null.
* @param bool $force_cache Whether to force an update of the local cache from the persistent cache. Default false.
$alloptions = apply_filters( 'pre_wp_load_alloptions', null, $force_cache );
if ( is_array( $alloptions ) ) {
if ( ! wp_installing() || ! is_multisite() ) {
$alloptions = wp_cache_get( 'alloptions', 'options', $force_cache );
$suppress = $wpdb->suppress_errors();
$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload IN ( '" . implode( "', '", esc_sql( wp_autoload_values_to_autoload() ) ) . "' )" );
if ( ! $alloptions_db ) {
$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
$wpdb->suppress_errors( $suppress );
foreach ( (array) $alloptions_db as $o ) {
$alloptions[ $o->option_name ] = $o->option_value;
if ( ! wp_installing() || ! is_multisite() ) {
* Filters all options before caching them.
* @param array $alloptions Array with all options.
$alloptions = apply_filters( 'pre_cache_alloptions', $alloptions );
wp_cache_add( 'alloptions', $alloptions, 'options' );
* Filters all options after retrieving them.
* @param array $alloptions Array with all options.
return apply_filters( 'alloptions', $alloptions );
* Primes specific network options for the current network into the cache with a single database query.
* Only network options that do not already exist in cache will be loaded.
* If site is not multisite, then call wp_prime_option_caches().
* @see wp_prime_network_option_caches()
* @param string[] $options An array of option names to be loaded.
function wp_prime_site_option_caches( array $options ) {
wp_prime_network_option_caches( null, $options );
* Primes specific network options into the cache with a single database query.
* Only network options that do not already exist in cache will be loaded.
* If site is not multisite, then call wp_prime_option_caches().
* @global wpdb $wpdb WordPress database abstraction object.
* @param int|null $network_id ID of the network. Can be null to default to the current network ID.
* @param string[] $options An array of option names to be loaded.
function wp_prime_network_option_caches( $network_id, array $options ) {
if ( ! is_multisite() ) {
wp_prime_option_caches( $options );
if ( $network_id && ! is_numeric( $network_id ) ) {
$network_id = (int) $network_id;
// Fallback to the current network if a network ID is not specified.
$network_id = get_current_network_id();
foreach ( $options as $option ) {
$cache_keys[ $option ] = "{$network_id}:{$option}";
$cache_group = 'site-options';
$cached_options = wp_cache_get_multiple( array_values( $cache_keys ), $cache_group );
$notoptions_key = "$network_id:notoptions";
$notoptions = wp_cache_get( $notoptions_key, $cache_group );
if ( ! is_array( $notoptions ) ) {
// Filter options that are not in the cache.
$options_to_prime = array();
foreach ( $cache_keys as $option => $cache_key ) {
( ! isset( $cached_options[ $cache_key ] ) || false === $cached_options[ $cache_key ] )
&& ! isset( $notoptions[ $option ] )
$options_to_prime[] = $option;
// Bail early if there are no options to be loaded.
if ( empty( $options_to_prime ) ) {
$query_args = $options_to_prime;
$query_args[] = $network_id;
$results = $wpdb->get_results(
"SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN (%s) AND site_id = %s",
implode( ',', array_fill( 0, count( $options_to_prime ), '%s' ) ),
$options_found = array();
foreach ( $results as $result ) {
$key = $result->meta_key;
$cache_key = $cache_keys[ $key ];
$data[ $cache_key ] = maybe_unserialize( $result->meta_value );
wp_cache_set_multiple( $data, $cache_group );
// If all options were found, no need to update `notoptions` cache.
if ( count( $options_found ) === count( $options_to_prime ) ) {
$options_not_found = array_diff( $options_to_prime, $options_found );
// Add the options that were not found to the cache.
$update_notoptions = false;
foreach ( $options_not_found as $option_name ) {
if ( ! isset( $notoptions[ $option_name ] ) ) {
$notoptions[ $option_name ] = true;
$update_notoptions = true;
// Only update the cache if it was modified.
if ( $update_notoptions ) {
wp_cache_set( $notoptions_key, $notoptions, $cache_group );
* Loads and primes caches of certain often requested network options if is_multisite().
* @since 6.3.0 Also prime caches for network options when persistent object cache is enabled.
* @since 6.6.0 Uses wp_prime_network_option_caches().
* @param int $network_id Optional. Network ID of network for which to prime network options cache. Defaults to current network.
function wp_load_core_site_options( $network_id = null ) {
if ( ! is_multisite() || wp_installing() ) {
$core_options = array( 'site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled', 'ms_files_rewriting', 'WPLANG' );
wp_prime_network_option_caches( $network_id, $core_options );
* Updates the value of an option that was already added.
* You do not need to serialize values. If the value needs to be serialized,
* then it will be serialized before it is inserted into the database.
* Remember, resources cannot be serialized or added as an option.
* If the option does not exist, it will be created.
* This function is designed to work with or without a logged-in user. In terms of security,
* plugin developers should check the current user's capabilities before updating any options.
* @since 4.2.0 The `$autoload` parameter was added.
* @since 6.7.0 The autoload values 'yes' and 'no' are deprecated.
* @global wpdb $wpdb WordPress database abstraction object.
* @param string $option Name of the option to update. Expected to not be SQL-escaped.
* @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
* @param bool|null $autoload Optional. Whether to load the option when WordPress starts up.
* Accepts a boolean, or `null` to stick with the initial value or, if no initial value is
* set, to leave the decision up to default heuristics in WordPress.
* For existing options, `$autoload` can only be updated using `update_option()` if `$value`
* For backward compatibility 'yes' and 'no' are also accepted, though using these values is
* Autoloading too many options can lead to performance problems, especially if the
* options are not frequently used. For options which are accessed across several places
* in the frontend, it is recommended to autoload them, by using true.
* For options which are accessed only on few specific URLs, it is recommended
* to not autoload them, by using false.
* For non-existent options, the default is null, which means WordPress will determine
* @return bool True if the value was updated, false otherwise.
function update_option( $option, $value, $autoload = null ) {
if ( is_scalar( $option ) ) {
$option = trim( $option );
if ( empty( $option ) ) {
* Until a proper _deprecated_option() function can be introduced,
* redirect requests to deprecated keys to the new, correct ones.
$deprecated_keys = array(
'blacklist_keys' => 'disallowed_keys',
'comment_whitelist' => 'comment_previously_approved',
if ( isset( $deprecated_keys[ $option ] ) && ! wp_installing() ) {
/* translators: 1: Deprecated option key, 2: New option key. */
__( 'The "%1$s" option key has been renamed to "%2$s".' ),
$deprecated_keys[ $option ]
return update_option( $deprecated_keys[ $option ], $value, $autoload );
wp_protect_special_option( $option );
if ( is_object( $value ) ) {
$value = sanitize_option( $option, $value );
$old_value = get_option( $option );
* Filters a specific option before its value is (maybe) serialized and updated.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* @since 4.4.0 The `$option` parameter was added.
* @param mixed $value The new, unserialized option value.
* @param mixed $old_value The old option value.
* @param string $option Option name.
$value = apply_filters( "pre_update_option_{$option}", $value, $old_value, $option );
* Filters an option before its value is (maybe) serialized and updated.
* @param mixed $value The new, unserialized option value.
* @param string $option Name of the option.
* @param mixed $old_value The old option value.
$value = apply_filters( 'pre_update_option', $value, $option, $old_value );
* If the new and old values are the same, no need to update.
* Unserialized values will be adequate in most cases. If the unserialized
* data differs, the (maybe) serialized data is checked to avoid
* unnecessary database calls for otherwise identical object instances.
* See https://core.trac.wordpress.org/ticket/38903
if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
/** This filter is documented in wp-includes/option.php */
if ( apply_filters( "default_option_{$option}", false, $option, false ) === $old_value ) {
return add_option( $option, $value, '', $autoload );
$serialized_value = maybe_serialize( $value );
* Fires immediately before an option value is updated.
* @param string $option Name of the option to update.
* @param mixed $old_value The old option value.
* @param mixed $value The new option value.
do_action( 'update_option', $option, $old_value, $value );
'option_value' => $serialized_value,
if ( null !== $autoload ) {
$update_args['autoload'] = wp_determine_option_autoload_value( $option, $value, $serialized_value, $autoload );
// Retrieve the current autoload value to reevaluate it in case it was set automatically.
$raw_autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
$allow_values = array( 'auto-on', 'auto-off', 'auto' );
if ( in_array( $raw_autoload, $allow_values, true ) ) {
$autoload = wp_determine_option_autoload_value( $option, $value, $serialized_value, $autoload );
if ( $autoload !== $raw_autoload ) {
$update_args['autoload'] = $autoload;
$result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
$notoptions = wp_cache_get( 'notoptions', 'options' );
if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
unset( $notoptions[ $option ] );
wp_cache_set( 'notoptions', $notoptions, 'options' );
if ( ! wp_installing() ) {
if ( ! isset( $update_args['autoload'] ) ) {
// Update the cached value based on where it is currently cached.
$alloptions = wp_load_alloptions( true );
if ( isset( $alloptions[ $option ] ) ) {
$alloptions[ $option ] = $serialized_value;
wp_cache_set( 'alloptions', $alloptions, 'options' );
wp_cache_set( $option, $serialized_value, 'options' );
} elseif ( in_array( $update_args['autoload'], wp_autoload_values_to_autoload(), true ) ) {
// Delete the individual cache, then set in alloptions cache.
wp_cache_delete( $option, 'options' );
$alloptions = wp_load_alloptions( true );
$alloptions[ $option ] = $serialized_value;
wp_cache_set( 'alloptions', $alloptions, 'options' );
// Delete the alloptions cache, then set the individual cache.
$alloptions = wp_load_alloptions( true );
if ( isset( $alloptions[ $option ] ) ) {
unset( $alloptions[ $option ] );