get_option( string $option, mixed $default = false )

Retrieves an option value based on an option name.


Description Description

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.


Parameters Parameters

$option

(string) (Required) Name of option to retrieve. Expected to not be SQL-escaped.

$default

(mixed) (Optional) Default value to return if the option does not exist.

Default value: false


Top ↑

Return Return

(mixed) Value set for the option.


Top ↑

Source Source

File: wp-includes/option.php

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function get_option( $option, $default = false ) {
    global $wpdb;
 
    $option = trim( $option );
    if ( empty( $option ) ) {
        return false;
    }
 
    /**
     * Filters the value of an existing option before it is retrieved.
     *
     * The dynamic portion of the hook name, `$option`, refers to the option name.
     *
     * Passing a truthy value to the filter will short-circuit retrieving
     * the option value, returning 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 bool|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 is 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' );
                } 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' ) ) ) {
        $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 );
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.

Top ↑

More Information More Information

A concise list of commonly-used options is below, but a more complete one can be found at the Option Reference.

  • 'admin_email' – E-mail address of blog administrator.
  • 'blogname' – Weblog title; set in General Options.
  • 'blogdescription' – Tagline for your blog; set in General Options.
  • 'blog_charset' – Character encoding for your blog; set in Reading Options.
  • 'date_format' – Default date format; set in General Options.
  • 'default_category' – Default post category; set in Writing Options.
  • 'home' – The blog’s home web address; set in General Options.
  • 'siteurl' – WordPress web address; set in General Options.
    Warning: This is not the same as get_bloginfo( 'url' ) (which will return the homepage url), but as get_bloginfo( 'wpurl' ).
  • 'template' – The current theme’s name; set in Presentation.
  • 'start_of_week' – Day of week calendar should start on; set in General Options.
  • 'upload_path' – Default upload location; set in Miscellaneous Options.
  • 'users_can_register' – Whether users can register; set in General Options.
  • 'posts_per_page' – Maximum number of posts to show on a page; set in Reading Options.
  • 'posts_per_rss' – Maximum number of most recent posts to show in the syndication feed; set in Reading Options.

There are many more options available, a lot of which depend on what plugins you have installed.



Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Handling of non-existing options

    1
    2
    3
    4
    5
    $no_exists_value = get_option( 'no_exists_value' );
    var_dump( $no_exists_value ); /* outputs false */
     
    $no_exists_value = get_option( 'no_exists_value', 'default_value' );
    var_dump( $no_exists_value ); /* outputs 'default_value' */
  2. Skip to note 5 content
    Contributed by ptasker

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

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

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    //Outputs an array of all plugins
     var_dump( get_option('active_plugins') );
     
     add_filter( 'option_active_plugins', function( $plugins ){
        return [];
    });
     
    //Outputs an empty array
    var_dump( get_option('active_plugins') );

    Helpful for disabling specific plugins when required.

    https://developer.wordpress.org/reference/hooks/option_option/

  3. Skip to note 7 content
    Contributed by Anurag soni

    Just like we use

    1
    get_option('date_format')

    to incorporate the date format defined in Settings -> General,

    We can also use

    1
    get_option('time_format')

    to incorporate the time format defined in Settings -> General.

    for example

    1
    <?php the_time(get_option('date_format')); ?>
    1
    <?php the_time(get_option('time_format')); ?>
  4. Skip to note 8 content
    Contributed by tradesouthwest

    There is a “long-hand” format for creating option settings-field parameter in one line that is harder to read/follow semantically, but provides a single string to a options setting instead of two strings for every option.

    1
    $wpdevref_text_2 = esc_attr( get_option( 'wpdevref_options' )['wpdevref_text_field_2'] );

    The above returns false, so technically there is no default to setup unless you require a value.
    And there are just a bit less parameter parsing than

    1
    2
    3
    $options = get_option('wpdevref_options');
    $wpdevref_text_2 = (empty($options['wpdevref_text_2'] ))
                     ? 'Default Text' : $options['wpdevref_text_2'];

    Alternatively (if default required):

    1
    2
    $wpdevref_text_2 = (empty( get_option( 'wpdevref_options' )['wpdevref_text_field_2']) )
                     ? 'Default Text' : get_option( 'wpdevref_options' )['wpdevref_text_field_2'];

    And for a checkbox in a settings array, try:

    1
    2
    3
    'value'   => get_option('wpdevref_fields')['wpdevref_dashnews_widgetcheck'],
    'checked' => esc_attr( checked( 1,
                 get_option('wpdevref_fields')['wpdevref_dashnews_widgetcheck'], false ) );
  5. Skip to note 9 content
    Contributed by venkat

    check options exists or not, use isset to check
    if value exists then checked box appear, if there is no option then unchecked box appears

    1
    2
    3
    4
    5
    6
    7
    $checkbox = get_option('options');
     
        if ( isset( $checkbox['check_box'] ) ) {
            echo 'value exists- so return checked box ';
        } else {
            echo ' no value  - so return unchecked box ';
        }

You must log in before being able to contribute a note or feedback.