get_option()用于获取通过option表单设置值得方法,如果数据库中不存在该选项,或者该选项的值为空,则返回FALSE.
【函数使用】
【参数说明】
$option
(string) (必须) 查询选项的名称. 数据库中已存在的选项名称如下(这些都在后台的“菜单”=》“常规”):
默认: None
$default
(mixed) (可选) 当数据库中不存在该选项默认的返回值.
Default: false
指定选项名称的值,如果没有返回数组则返回FALSE.更多信息请见http://codex.wordpress.org/Option_Reference
【函数实例】
显示博客标题:
显示博客站点字符集:
Character set:
获取管理员email地址:
【源代码】
get_option() 位于 wp-includes/option.php,函数代码如下:
function get_option( $option, $default = false ) {
global $wpdb;
$option = trim( $option );
if ( empty( $option ) )
return false;
// Allow plugins to short-circuit options.
$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] ) )
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' );
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
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 );
return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
}