get_option (获取选项)

描述

从数据库表中获取选项的值,如果所需选项不存在,或选项无相关值,函数返回FALSE。

语法

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

参数

$option (string) (Required) 获取选项的名称

$default (mixed) (Optional) (Default value: false) 如果选项不存在默认返回的值

系统默认参数:

'admin_email' – 管理员的E-mail地址.
'blogname' – 网站title标题.
'blogdescription' – 网站描述.
'blog_charset' – 网站编码一般UTF-8.
'date_format' – 日期格式.
'default_category' – 文章默认分类.
'home' – 网站地址.
'siteurl' – wordpress的web地址.
注意:此选项与函数 get_bloginfo('siteurl')不同 get_bloginfo('siteurl')返回当前站地址, 但是这个选项等同于get_bloginfo('wpurl');.
'template' -当前主题名称
'start_of_week' -一星期开始设置
'upload_path' – 上传默认目录.
'posts_per_page' – 文章分页每页显示的数量.
'posts_per_rss' – RSS聚合显示的最新文章数量

源码

位置:wp-includes/option.php

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 );
}