* Retrieves an option value based on an option name.
* If the option does not exist, and a default value is not provided,
* boolean false is returned. This could be used to check whether you need
* to initialize an option during installation of a plugin, however that
* can be done better by using add_option() which will not overwrite
* Not initializing an option and using boolean `false` as a return value
* is a bad practice as it triggers an additional database query.
* The type of the returned value can be different from the type that was passed
* when saving or updating the option. If the option value was serialized,
* then it will be unserialized when it is returned. In this case the type will
* be the same. For example, storing a non-scalar value like an array will
* In most cases non-string scalar and null values will be converted and returned
* 1. When the option has not been saved in the database, the `$default_value` value
* is returned if provided. If not, boolean `false` is returned.
* 2. When one of the Options API filters is used: {@see 'pre_option_$option'},
* {@see 'default_option_$option'}, or {@see 'option_$option'}, the returned
* value may not match the expected type.
* 3. When the option has just been saved in the database, and get_option()
* is used right after, non-string scalar and null values are not converted to
* string equivalents and the original type is returned.
* When adding options like this: `add_option( 'my_option_name', 'value' )`
* and then retrieving them with `get_option( 'my_option_name' )`, the returned
* - `false` returns `string(0) ""`
* - `true` returns `string(1) "1"`
* - `0` returns `string(1) "0"`
* - `1` returns `string(1) "1"`
* - `'0'` returns `string(1) "0"`
* - `'1'` returns `string(1) "1"`
* - `null` returns `string(0) ""`
* When adding options with non-scalar values like
* `add_option( 'my_array', array( false, 'str', null ) )`, the returned value
* will be identical to the original as it is serialized before saving
* @global wpdb $wpdb WordPress database abstraction object.
* @param string $option Name of the option to retrieve. Expected to not be SQL-escaped.
* @param mixed $default_value Optional. Default value to return if the option does not exist.
* @return mixed Value of the option. A value of any type may be returned, including
* scalar (string, boolean, float, integer), null, array, object.
* Scalar and null values will be returned as strings as long as they originate
* from a database stored option value. If there is no option in the database,
* boolean `false` is returned.
function get_option( $option, $default_value = false ) {
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 get_option( $deprecated_keys[ $option ], $default_value );
* Filters the value of an existing option before it is retrieved.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* Returning a value other than false from the filter will short-circuit retrieval
* and return that value instead.
* @since 4.4.0 The `$option` parameter was added.
* @since 4.9.0 The `$default_value` parameter was added.
* @param mixed $pre_option The value to return instead of the option value. This differs from
* `$default_value`, which is used as the fallback value in the event
* the option doesn't exist elsewhere in get_option().
* Default false (to skip past the short-circuit).
* @param string $option Option name.
* @param mixed $default_value The fallback value to return if the option does not exist.
$pre = apply_filters( "pre_option_{$option}", false, $option, $default_value );
* Filters the value of any existing option before it is retrieved.
* Returning a value other than false from the filter will short-circuit retrieval
* and return that value instead.
* @param mixed $pre_option The value to return instead of the option value. This differs from
* `$default_value`, which is used as the fallback value in the event
* the option doesn't exist elsewhere in get_option().
* Default false (to skip past the short-circuit).
* @param string $option Name of the option.
* @param mixed $default_value The fallback value to return if the option does not exist.
$pre = apply_filters( 'pre_option', $pre, $option, $default_value );
if ( defined( 'WP_SETUP_CONFIG' ) ) {
// Distinguish between `false` as a default, and not passing one.
$passed_default = func_num_args() > 1;
if ( ! wp_installing() ) {
$alloptions = wp_load_alloptions();
* When getting an option value, we check in the following order for performance:
* 1. Check the 'alloptions' cache first to prioritize existing loaded options.
* 2. Check the 'notoptions' cache before a cache lookup or DB hit.
* 3. Check the 'options' cache prior to a DB hit.
* 4. Check the DB for the option and cache it in either the 'options' or 'notoptions' cache.
if ( isset( $alloptions[ $option ] ) ) {
$value = $alloptions[ $option ];
// Check for non-existent options first to avoid unnecessary object cache lookups and DB hits.
$notoptions = wp_cache_get( 'notoptions', 'options' );
if ( ! is_array( $notoptions ) ) {
wp_cache_set( 'notoptions', $notoptions, 'options' );
if ( isset( $notoptions[ $option ] ) ) {
* Filters the default value for an option.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value.
* @param mixed $default_value The default value to return if the option does not exist
* @param string $option Option name.
* @param bool $passed_default Was `get_option()` passed a default value?
return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
$value = wp_cache_get( $option, 'options' );
if ( false === $value ) {
$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
// Has to be get_row() instead of get_var() because of funkiness with 0, false, null values.
if ( is_object( $row ) ) {
$value = $row->option_value;
wp_cache_add( $option, $value, 'options' );
} else { // Option does not exist, so we must cache its non-existence.
$notoptions[ $option ] = true;
wp_cache_set( 'notoptions', $notoptions, 'options' );
/** This filter is documented in wp-includes/option.php */
return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
$suppress = $wpdb->suppress_errors();
$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
$wpdb->suppress_errors( $suppress );
if ( is_object( $row ) ) {
$value = $row->option_value;
/** This filter is documented in wp-includes/option.php */
return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
// If home is not set, use siteurl.
if ( 'home' === $option && '' === $value ) {
return get_option( 'siteurl' );
if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ), true ) ) {
$value = untrailingslashit( $value );
* Filters the value of an existing option.
* The dynamic portion of the hook name, `$option`, refers to the option name.
* @since 1.5.0 As 'option_' . $setting
* @since 4.4.0 The `$option` parameter was added.
* @param mixed $value Value of the option. If stored serialized, it will be
* unserialized prior to being returned.
* @param string $option Option name.
return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
* Primes specific options into the cache with a single database query.
* Only options that do not already exist in cache will be loaded.
* @global wpdb $wpdb WordPress database abstraction object.
* @param string[] $options An array of option names to be loaded.
function wp_prime_option_caches( $options ) {
$alloptions = wp_load_alloptions();
$cached_options = wp_cache_get_multiple( $options, 'options' );
$notoptions = wp_cache_get( 'notoptions', 'options' );
if ( ! is_array( $notoptions ) ) {
// Filter options that are not in the cache.
$options_to_prime = array();
foreach ( $options as $option ) {
( ! isset( $cached_options[ $option ] ) || false === $cached_options[ $option ] )
&& ! isset( $alloptions[ $option ] )
&& ! isset( $notoptions[ $option ] )
$options_to_prime[] = $option;
// Bail early if there are no options to be loaded.
if ( empty( $options_to_prime ) ) {
$results = $wpdb->get_results(
"SELECT option_name, option_value FROM $wpdb->options WHERE option_name IN (%s)",
implode( ',', array_fill( 0, count( $options_to_prime ), '%s' ) )
$options_found = array();
foreach ( $results as $result ) {
* The cache is primed with the raw value (i.e. not maybe_unserialized).
* `get_option()` will handle unserializing the value as needed.
$options_found[ $result->option_name ] = $result->option_value;
wp_cache_set_multiple( $options_found, 'options' );
// 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, array_keys( $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', $notoptions, 'options' );
* Primes the cache of all options registered with a specific option group.
* @global array $new_allowed_options
* @param string $option_group The option group to load options for.
function wp_prime_option_caches_by_group( $option_group ) {
global $new_allowed_options;
if ( isset( $new_allowed_options[ $option_group ] ) ) {
wp_prime_option_caches( $new_allowed_options[ $option_group ] );
* Retrieves multiple options.
* Options are loaded as necessary first in order to use a single database query at most.
* @param string[] $options An array of option names to retrieve.
* @return array An array of key-value pairs for the requested options.
function get_options( $options ) {
wp_prime_option_caches( $options );
foreach ( $options as $option ) {
$result[ $option ] = get_option( $option );
* Sets the autoload values for multiple options in the database.
* 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.
* @since 6.7.0 The autoload values 'yes' and 'no' are deprecated.
* @global wpdb $wpdb WordPress database abstraction object.
* @param array $options 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 array Associative array of all provided $options as keys and boolean values for whether their autoload value
function wp_set_option_autoload_values( array $options ) {
$grouped_options = 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;
$grouped_options['on'][] = $option;
$results[ $option ] = false; // Initialize result value.
foreach ( $grouped_options as $autoload => $options ) {
$placeholders = implode( ',', array_fill( 0, count( $options ), '%s' ) );
$where[] = "autoload != '%s' AND option_name IN ($placeholders)";
$where_args[] = $autoload;
foreach ( $options as $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 ) {
// Run UPDATE queries as needed (maximum 2) to update the relevant options' autoload values to 'yes' or 'no'.
foreach ( $grouped_options as $autoload => $options ) {
$options = array_intersect( $options, $options_to_update );
$grouped_options[ $autoload ] = $options;
if ( ! $grouped_options[ $autoload ] ) {
// Run query to update autoload value for all the options where it is needed.
"UPDATE $wpdb->options SET autoload = %s WHERE option_name IN (" . implode( ',', array_fill( 0, count( $grouped_options[ $autoload ] ), '%s' ) ) . ')',
$grouped_options[ $autoload ]
// Set option list to an empty array to indicate no options were updated.
$grouped_options[ $autoload ] = array();
// 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
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 ] );