• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / get_option() – Retrieves an option value based on an option name.

You appear to be a bot. Output may be restricted

Description

Retrieves an option value based on an option name.

If the option does not exist or does not have a value, then the return value will be false. This is useful to check whether you need to install an option and is commonly used during installation of plugin options and to test whether upgrading is required. If the option was serialized then it will be unserialized when it is returned. Any scalar values will be returned as strings. You may coerce the return type of a given option by registering an option_$option filter callback.

Usage

$mixed = get_option( $option, $default );

Parameters

$option
( string ) required – Name of the option to retrieve. Expected to not be SQL-escaped.
$default
( mixed ) optional – Optional. Default value to return if the option does not exist.

Returns

mixed Value set for the option. A value of any type may be returned, including array, boolean, float, integer, null, object, and string.

Source

File name: wordpress/wp-includes/option.php
Lines:

1 to 100 of 150
function get_option( $option, $default = false ) {
  global $wpdb;

  $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 ( ! wp_installing() && isset( $deprecated_keys[ $option ] ) ) {
    _deprecated_argument(
      get_option,
      '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 );
  }

  
/**
 * 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 truthy value from the filter will effectively short-circuit retrieval
 * and return the passed value instead.
 *
 * @since 1.5.0
 * @since 4.4.0 The `$option` parameter was added.
 * @since 4.9.0 The `$default` parameter was added.
 *
 * @param mixed  $pre_option The value to return instead of the option value. This differs
 *                           from `$default`, 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    The fallback value to return if the option does not exist.
 *                           Default false.
 */
  $pre = apply_filters( "pre_option_{$option}", false, $option, $default );

  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() ) {
    // Prevent non-existent options from triggering multiple queries.
    $notoptions = wp_cache_get( '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 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, $option, $passed_default );
    }

    $alloptions = wp_load_alloptions();

    if ( isset( $alloptions[ $option ] ) ) {
      $value = $alloptions[ $option ];
    } else {
      $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' );
 
[1] [2] Next »

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 21st August 2020

Primary Sidebar

Information

Function name: get_option
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/option.php
File ref: wp-includes/option.php
Deprecated?: No
API Letters: G,O

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites