WordPress使用get_option()来获取通过option表单设置值的方法,如果数据库中不存在该选项,或者改选项的值为空,那么将会返回一个false。
通过get_option获得的值通常都可以在后台的"菜单"->"常规"中获得。而且它们内容如下:
admin_email 管理员的email地址
blogname 网站title标题
blogscription 网站描述
blog_charset 网站编码,一般都是utf-8
date_format 日期格式
default_category 文章默认分类
home 网站地址
siteurl WordPress的web地址
template 当前主题名称
start_of-week 星期开始设置
upload_path 上传默认目录
posts_per_page 文章分页每页显示的数量
posts_per_rss rss聚合显示的最新文章数量
需要注意的是该函数可以接受两个参数,如果查询的第一个参数不存在时,就会返回第二个参数。
如果只写一个参数,那么当查询的第一个参数不存在时,将会返回false。
get-option()位于wp-includes/option.php文件中,其函数的源代码部分(3.9版本)如下:
/** * Option API * * @package WordPress * @subpackage Option */ /** * Retrieve option value based on name of option. * * 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. * * @since 1.5.0 * * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. * @param mixed $default Optional. Default value to return if the option does not exist. * @return mixed Value set for the option. */ function get_option( $option, $default = false ) { global $wpdb; $option = trim( $option ); if ( empty( $option ) ) return false; /** * Filter 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 * * @param bool|mixed $pre_option Value to return instead of the option value. * Default false to skip it. */ $pre = apply_filters( 'pre_option_' . $option, false ); if ( false !== $pre ) return $pre; if ( defined( 'WP_SETUP_CONFIG' ) ) return false; if ( ! defined( 'WP_INSTALLING' ) ) { // prevent non-existent options from triggering multiple queries $notoptions = wp_cache_get( 'notoptions', 'options' ); if ( isset( $notoptions[$option] ) ) /** * Filter the default value for an option. * * The dynamic portion of the hook name, $option, refers * to the option name. * * @since 3.4.0 * * @param mixed $default The default value to return if the option * does not exist in the database. */ return apply_filters( 'default_option_' . $option, $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 $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 ); } } } } 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 ); } } // 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 ); /** * Filter 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 * * @param mixed $value Value of the option. If stored serialized, it will be * unserialized prior to being returned. */ return apply_filters( 'option_' . $option, maybe_unserialize( $value ) ); }
时间: 2025-01-18 05:39:58