get_option

The WordPress Core get option function.

Description

get_option(); 

Usage

  1. if ( !function_exists( 'get_option' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/noop.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = get_option(); 
  7.  

Defined (3)

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

/wp-admin/includes/noop.php  
  1. function get_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-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. // Distinguish between `false` as a default, and not passing one. 
  27. $passed_default = func_num_args() > 1; 
  28.  
  29. if ( ! wp_installing() ) { 
  30. // prevent non-existent options from triggering multiple queries 
  31. $notoptions = wp_cache_get( 'notoptions', 'options' ); 
  32. if ( isset( $notoptions[ $option ] ) ) { 
  33. /** 
  34. * Filters the default value for an option. 
  35. * The dynamic portion of the hook name, `$option`, refers to the option name. 
  36. * @since 3.4.0 
  37. * @since 4.4.0 The `$option` parameter was added. 
  38. * @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value. 
  39. * @param mixed $default The default value to return if the option does not exist 
  40. * in the database. 
  41. * @param string $option Option name. 
  42. * @param bool $passed_default Was `get_option()` passed a default value? 
  43. */ 
  44. return apply_filters( "default_option_{$option}", $default, $option, $passed_default ); 
  45.  
  46. $alloptions = wp_load_alloptions(); 
  47.  
  48. if ( isset( $alloptions[$option] ) ) { 
  49. $value = $alloptions[$option]; 
  50. } else { 
  51. $value = wp_cache_get( $option, 'options' ); 
  52.  
  53. if ( false === $value ) { 
  54. $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); 
  55.  
  56. // Has to be get_row instead of get_var because of funkiness with 0, false, null values 
  57. if ( is_object( $row ) ) { 
  58. $value = $row->option_value; 
  59. wp_cache_add( $option, $value, 'options' ); 
  60. } else { // option does not exist, so we must cache its non-existence 
  61. if ( ! is_array( $notoptions ) ) { 
  62. $notoptions = array(); 
  63. $notoptions[$option] = true; 
  64. wp_cache_set( 'notoptions', $notoptions, 'options' ); 
  65.  
  66. /** This filter is documented in wp-includes/option.php */ 
  67. return apply_filters( 'default_option_' . $option, $default, $option, $passed_default ); 
  68. } else { 
  69. $suppress = $wpdb->suppress_errors(); 
  70. $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); 
  71. $wpdb->suppress_errors( $suppress ); 
  72. if ( is_object( $row ) ) { 
  73. $value = $row->option_value; 
  74. } else { 
  75. /** This filter is documented in wp-includes/option.php */ 
  76. return apply_filters( 'default_option_' . $option, $default, $option, $passed_default ); 
  77.  
  78. // If home is not set use siteurl. 
  79. if ( 'home' == $option && '' == $value ) 
  80. return get_option( 'siteurl' ); 
  81.  
  82. if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) ) 
  83. $value = untrailingslashit( $value ); 
  84.  
  85. /** 
  86. * Filters the value of an existing option. 
  87. * The dynamic portion of the hook name, `$option`, refers to the option name. 
  88. * @since 1.5.0 As 'option_' . $setting 
  89. * @since 3.0.0 
  90. * @since 4.4.0 The `$option` parameter was added. 
  91. * @param mixed $value Value of the option. If stored serialized, it will be 
  92. * unserialized prior to being returned. 
  93. * @param string $option Option name. 
  94. */ 
  95. return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );