• 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:

101 to 150 of 150
        } else { // Option does not exist, so we must cache its non-existence.
          if ( ! is_array( $notoptions ) ) {
            $notoptions = array();
          }

          $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, $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, $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 );
}
 
« Previous [1] [2]

 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