WordPress函数:get_option 获取配置选项值

江河/ 2023年08月19日/ WordPress/ 浏览 695

函数原型:


get_option( string $option, mixed $default_value = false ): mixed


获取基于选项名称的选项值。


如果该选项不存在,并且没有提供默认值,则返回布尔值false。这可以用来检查在安装插件期间是否需要初始化一个选项,但是使用add_option()可以做得更好,因为它不会覆盖现有的选项。


不初始化选项并使用boolean false作为返回值是一种糟糕的做法,因为它会触发额外的数据库查询。


返回值的类型可以与保存或更新选项时传递的类型不同。如果选项值已序列化,则返回时将不序列化。在这种情况下,类型将是相同的。例如,像数组一样存储非标量值将返回相同的数组。


在大多数情况下,非字符串标量和null值将被转换并作为字符串等价物返回。


异常情况:


1. 如果该选项尚未保存在数据库中,则会返回$default_value值(如果提供)。否则,返回布尔值false。


2. 如果使用了选项API筛选器之一:“pre_option_$option”、“default_option\u$option“或”“option_$Options”,则返回的值可能与预期类型不匹配。


3. 当选项刚刚保存在数据库中,并且紧接着使用get_option()时,非字符串标量和null值不会转换为字符串等价值,而是返回原始类型。


举例:


当添加add_option('my_option_','value')这样的选项,然后使用get_option('my_opties_')检索它们时,返回的值将是:


false returns string(0) ""


true returns string(1) "1"


0 returns string(1) "0"


1 returns string(1) "1"


'0' returns string(1) "0"


'1' returns string(1) "1"


null returns string(0) ""


当添加具有非标量值的选项(如add_option('my_array',array(false,'str',null))时,返回的值将与原始值相同,因为它在将其保存到数据库之前进行了序列化:


array(3) {
    [0] => bool(false)
    [1] => string(3) "str"
    [2] => NULL
}


参数说明:


$option,要检索的选项的名称。应为非SQL转义。


$default_value,如果选项不存在,则返回默认值。


特别提示:


有一些 key 在 WordPress 中已被使用,而且大多数主题插件中也会使用


admin_email – 博客管理员的电子邮件地址。


blogname – 日志标题;在“常规设置”中设置。


blogdescription – 您博客的标语;在“常规设置”中设置。


blog_charset – 博客的字符编码;设置。


date_format – 默认日期格式;在“常规设置”中设置。


default_category – 默认帖子类别;设置。


home – 博客的主页网址;在“常规设置”中设置。


posts_per_page – 页面上显示的最大帖子数;设置。


posts_per_rss – 要在联合提要中显示的最新帖子的最大数量;设置。


siteurl – WordPress网址;在“常规设置”中设置。警告:这与get_bloginfo('url')(将返回主页url)不同,而是与get_bloginfo('wpurl')。


template – 当前主题的名称。


start_of_week – 星期几日历应从开始;在“常规设置”中设置。


upload_path – 默认上传位置;设置。


users_can_regist – 用户是否可以注册;在“常规设置”中设置。


函数源码:


function get_option( $option, $default_value = false ) {
	global $wpdb;

	if ( is_scalar( $option ) ) {
		$option = trim( $option );
	}

	if ( empty( $option ) ) {
		return false;
	}

	$deprecated_keys = array(
		'blacklist_keys'    => 'disallowed_keys',
		'comment_whitelist' => 'comment_previously_approved',
	);

	if ( isset( $deprecated_keys[ $option ] ) && ! wp_installing() ) {
		_deprecated_argument(
			__FUNCTION__,
			'5.5.0',
			sprintf(
				/* translators: 1: Deprecated option key, 2: New option key. */
				__( 'The "%1$s" option key has been renamed to "%2$s".' ),
				$option,
				$deprecated_keys[ $option ]
			)
		);
		return get_option( $deprecated_keys[ $option ], $default_value );
	}

	$pre = apply_filters( "pre_option_{$option}", false, $option, $default_value );

	$pre = apply_filters( 'pre_option', $pre, $option, $default_value );

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

		// Prevent non-existent `notoptions` key from triggering multiple key lookups.
		if ( ! is_array( $notoptions ) ) {
			$notoptions = array();
			wp_cache_set( 'notoptions', $notoptions, 'options' );
		}

		if ( isset( $notoptions[ $option ] ) ) {
			return apply_filters( "default_option_{$option}", $default_value, $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_value, $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_value, $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' ), true ) ) {
		$value = untrailingslashit( $value );
	}

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


包含钩子:


apply_filters( "default_option_{$option}", mixed $default_value, string $option, bool $passed_default )

apply_filters( "option_{$option}", mixed $value, string $option )

apply_filters( 'pre_option', mixed $pre_option, string $option, mixed $default_value )

apply_filters( "pre_option_{$option}", mixed $pre_option, string $option, mixed $default_value )


使用举例:


$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' */


发表评论

暂无评论,抢个沙发...

客服 工单