get_option

Retrieves an option value based on an option name.

Description

get_option( (string) $option, (bool) $default = false ); 

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 filter callback.

Parameters (2)

0. $option (string)
Name of option to retrieve. Expected to not be SQL-escaped.
1. $default — Optional. (bool) => false
Default value to return if the option does not exist.

Usage

  1. if ( !function_exists( 'get_option' ) ) { 
  2. require_once ABSPATH . WPINC . '/option.php'; 
  3.  
  4. // Name of option to retrieve. Expected to not be SQL-escaped. 
  5. $option = ''; 
  6.  
  7. // Optional. Default value to return if the option does not exist. 
  8. $default = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_option($option, $default); 
  12.  

Defined (3)

The function is defined in the following location(s).

/wp-includes/option.php  
  1. function get_option( $option, $default = false ) { 
  2. global $wpdb; 
  3.  
  4. $option = trim( $option ); 
  5. if ( empty( $option ) ) 
  6. return false; 
  7.  
  8. /** 
  9. * Filters the value of an existing option before it is retrieved. 
  10. * The dynamic portion of the hook name, `$option`, refers to the option name. 
  11. * Passing a truthy value to the filter will short-circuit retrieving 
  12. * the option value, returning the passed value instead. 
  13. * @since 1.5.0 
  14. * @since 4.4.0 The `$option` parameter was added. 
  15. * @param bool|mixed $pre_option Value to return instead of the option value. 
  16. * Default false to skip it. 
  17. * @param string $option Option name. 
  18. */ 
  19. $pre = apply_filters( 'pre_option_' . $option, false, $option ); 
  20. if ( false !== $pre ) 
  21. return $pre; 
  22.  
  23. if ( defined( 'WP_SETUP_CONFIG' ) ) 
  24. return false; 
  25.  
  26. if ( ! wp_installing() ) { 
  27. // prevent non-existent options from triggering multiple queries 
  28. $notoptions = wp_cache_get( 'notoptions', 'options' ); 
  29. if ( isset( $notoptions[ $option ] ) ) { 
  30. /** 
  31. * Filters the default value for an option. 
  32. * The dynamic portion of the hook name, `$option`, refers to the option name. 
  33. * @since 3.4.0 
  34. * @since 4.4.0 The `$option` parameter was added. 
  35. * @param mixed $default The default value to return if the option does not exist 
  36. * in the database. 
  37. * @param string $option Option name. 
  38. */ 
  39. return apply_filters( 'default_option_' . $option, $default, $option ); 
  40.  
  41. $alloptions = wp_load_alloptions(); 
  42.  
  43. if ( isset( $alloptions[$option] ) ) { 
  44. $value = $alloptions[$option]; 
  45. } else { 
  46. $value = wp_cache_get( $option, 'options' ); 
  47.  
  48. if ( false === $value ) { 
  49. $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); 
  50.  
  51. // Has to be get_row instead of get_var because of funkiness with 0, false, null values 
  52. if ( is_object( $row ) ) { 
  53. $value = $row->option_value; 
  54. wp_cache_add( $option, $value, 'options' ); 
  55. } else { // option does not exist, so we must cache its non-existence 
  56. if ( ! is_array( $notoptions ) ) { 
  57. $notoptions = array(); 
  58. $notoptions[$option] = true; 
  59. wp_cache_set( 'notoptions', $notoptions, 'options' ); 
  60.  
  61. /** This filter is documented in wp-includes/option.php */ 
  62. return apply_filters( 'default_option_' . $option, $default, $option ); 
  63. } else { 
  64. $suppress = $wpdb->suppress_errors(); 
  65. $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); 
  66. $wpdb->suppress_errors( $suppress ); 
  67. if ( is_object( $row ) ) { 
  68. $value = $row->option_value; 
  69. } else { 
  70. /** This filter is documented in wp-includes/option.php */ 
  71. return apply_filters( 'default_option_' . $option, $default, $option ); 
  72.  
  73. // If home is not set use siteurl. 
  74. if ( 'home' == $option && '' == $value ) 
  75. return get_option( 'siteurl' ); 
  76.  
  77. if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) ) 
  78. $value = untrailingslashit( $value ); 
  79.  
  80. /** 
  81. * Filters the value of an existing option. 
  82. * The dynamic portion of the hook name, `$option`, refers to the option name. 
  83. * @since 1.5.0 As 'option_' . $setting 
  84. * @since 3.0.0 
  85. * @since 4.4.0 The `$option` parameter was added. 
  86. * @param mixed $value Value of the option. If stored serialized, it will be 
  87. * unserialized prior to being returned. 
  88. * @param string $option Option name. 
  89. */ 
  90. return apply_filters( 'option_' . $option, maybe_unserialize( $value ), $option ); 
/wp-admin/includes/upgrade.php  
  1. function __get_option($setting) { 
  2. global $wpdb; 
  3.  
  4. if ( $setting == 'home' && defined( 'WP_HOME' ) ) 
  5. return untrailingslashit( WP_HOME ); 
  6.  
  7. if ( $setting == 'siteurl' && defined( 'WP_SITEURL' ) ) 
  8. return untrailingslashit( WP_SITEURL ); 
  9.  
  10. $option = $wpdb->get_var( $wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting ) ); 
  11.  
  12. if ( 'home' == $setting && '' == $option ) 
  13. return __get_option( 'siteurl' ); 
  14.  
  15. if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting || 'tag_base' == $setting ) 
  16. $option = untrailingslashit( $option ); 
  17.  
  18. return maybe_unserialize( $option ); 
/wp-admin/includes/noop.php  
  1. function get_option() {}