get_option()WP 1.5.0

Gets the value of the specified setting (option).

The function caches data and on subsequent calls the data will be taken from the cache rather than from the DB.

If the option is stored as a serialized string, it will be automatically decoded when retrieved.

Any scalar values (true, 0, null) will be returned as strings.

The returned value can be filtered via the hook option_(option).

Use get_site_option() to get a network option (in multisite), not the current site (blog).

1 time — 0.0025661 sec (very slow) | 50000 times — 0.20 sec (very fast) | PHP 7.4.33, WP 6.4.1

Returns

Mixed.

  • string/array — the value of the option.
  • false — when the specified option does not exist or its value could not be retrieved.
  • Any type — when the option does not exist and a default value is specified (in the second parameter), the specified default value will be returned.
  • Any type — when a callback is attached to the filter pre_option_(option), default_option_(option), or option_(option).

The return type may vary:

  • Complex types (arrays, objects) are serialized and returned as is.
  • Simple values (true, 0, null) are most often returned as strings.

Exceptions:

  • If a $default_value is specified, it will be returned. If not — false.
  • Filters pre_option_*, default_option_*, option_* may alter the result.
  • Immediately after add_option() the original type is returned, not a string.

For example:

add_option( 'opt', 'val' );  // Saved
get_option( 'opt' );         // Retrieved

/*
Saved → Retrieved:

false  → ""  (string)
true   → "1" (string)
0      → "0" (string)
1      → "1" (string)
null   → ""  (string)
[ false, 'str', null ] → [ false, 'str', null ] (same array)
*/

Usage

get_option( $option, $default_value );
$option(string) (required)

The name of the option whose value needs to be retrieved. Some of the available options:

  • admin_email - The blog administrator's email.
  • blogname - The name of the blog. Set in the settings.
  • blogdescription - The description of the blog. Set in the settings.
  • blog_charset - The blog's character encoding. Set in the settings.
  • date_format - The date format. Set in the settings.
  • default_category - The default category for posts. Set in the settings.
  • home - The address of the blog's homepage. Set in the general settings.
  • siteurl - The address of WordPress. Set in the general settings.

    Note: siteurl is different from get_bloginfo('siteurl') (which returns the URL of the blog's homepage). And is not different from get_bloginfo('wpurl').

  • template - The name of the current theme.
  • start_of_week - The day the week starts. Set in the general settings.
  • upload_path - The default upload directory. Set in the settings.
  • posts_per_page - The maximum number of posts per page. Set in the reading settings.
  • posts_per_rss - The maximum number of posts displayed in the feed. Set in the reading settings.

A complete list of options can be found here.

$default_value(string/int/boolean)
The default value to return if the option does not exist in the DB.
Default: false

Examples

0

#1 Display the site name in H1 tag:

<h1><?php echo get_option('blogname'); ?></h1>

OR short variant:

<h1><?= get_option('blogname'); ?></h1>
0

#2 Display the site charset:

<p>Current charset: <?php echo get_option('blog_charset'); ?> </p>
0

#3 Place the site admin email into the $admin_email variable:

$admin_email = get_option('admin_email');
0

#4 Handling of non-existing options

$no_exists_value = get_option( 'no_exists_value' );
var_dump( $no_exists_value ); //> false

$no_exists_value = get_option( 'no_exists_value', 'default_value' );
var_dump( $no_exists_value ); //> 'default_value'
0

#5 Filter the result of get_option() in runtime

A quick tip that the output of get_option() is filterable:

return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );

So you can change the output of get_option() at run time.

var_dump( get_option('active_plugins') ); //> an array of all plugins

add_filter( 'option_active_plugins', function( $plugins ){
	return [];
} );

var_dump( get_option('active_plugins') ); //> empty array

Helpful for disabling specific plugins when required.

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 1.5.0 Introduced.

get_option() code WP 6.9.1

function get_option( $option, $default_value = false ) {
	global $wpdb;

	if ( is_scalar( $option ) ) {
		$option = trim( $option );
	}

	if ( empty( $option ) ) {
		return false;
	}

	/*
	 * 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() ) {
		_deprecated_argument(
			__FUNCTION__,
			'5.5.0',
			sprintf(
				/* translators: 1: Deprecated option key, 2: New option key. */
				__( 'The "%1$s" option key has been renamed to "%2$s".' ),
				$option,
				$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 1.5.0
	 * @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.
	 *                              Default false.
	 */
	$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.
	 *
	 * @since 6.1.0
	 *
	 * @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.
	 *                              Default false.
	 */
	$pre = apply_filters( 'pre_option', $pre, $option, $default_value );

	if ( false !== $pre ) {
		return $pre;
	}

	if ( defined( 'WP_SETUP_CONFIG' ) ) {
		return false;
	}

	// 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 ];
		} else {
			// 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 ) ) {
				$notoptions = array();
				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 3.4.0
				 * @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
				 *                               in the database.
				 * @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 );
				}
			}
		}
	} else {
		$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;
		} else {
			/** 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 3.0.0
	 * @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 );
}