1. 首页
  2. WordPress 函数手册

get_site_option()

get_site_option( string $option, mixed $default = false, bool $deprecated = true )

根据选项名称检索当前网络的选项值。
Retrieve an option value for the current network based on name of option.

目录锚点:#说明#参数#返回#源码#笔记


说明(Description)

另见函数 get_network_option()


参数(Parameters)

参数 类型 必填 说明
$option (string) 必需 要检索的选项的名称。应为非SQL转义。
$default (mixed) 可选 选项不存在时返回的值。
$deprecated (bool) 可选 是否使用缓存。仅限多站点。始终设置为true。

返回(Return)

(mixed)为选项设置的值。


源码(Source)

/**
 * Retrieve site option value based on name of option.
 *
 * @since 2.8.0
 *
 * @see get_option()
 *
 * @global wpdb $wpdb
 *
 * @param string $option    Name of option to retrieve. Expected to not be SQL-escaped.
 * @param mixed  $default   Optional value to return if option doesn't exist. Default false.
 * @param bool   $use_cache Whether to use cache. Multisite only. Default true.
 * @return mixed Value set for the option.
 */
function get_site_option( $option, $default = false, $use_cache = true ) {
	global $wpdb;

	/**
	 * Filter an existing site 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 effectively short-circuit retrieval,
	 * returning the passed value instead.
	 *
	 * @since 2.9.0 As 'pre_site_option_' . $key
	 * @since 3.0.0
	 *
	 * @param mixed $pre_option The default value to return if the option does not exist.
	 */
 	$pre = apply_filters( 'pre_site_option_' . $option, false );

 	if ( false !== $pre )
 		return $pre;

	// prevent non-existent options from triggering multiple queries
	$notoptions_key = "{$wpdb->siteid}:notoptions";
	$notoptions = wp_cache_get( $notoptions_key, 'site-options' );

	if ( isset( $notoptions[$option] ) ) {

		/**
		 * Filter a specific default site option.
		 *
		 * The dynamic portion of the hook name, `$option`, refers to the option name.
		 *
		 * @since 3.4.0
		 *
		 * @param mixed $default The value to return if the site option does not exist
		 *                       in the database.
		 */
		return apply_filters( 'default_site_option_' . $option, $default );
	}

	if ( ! is_multisite() ) {

		/** This filter is documented in wp-includes/option.php */
		$default = apply_filters( 'default_site_option_' . $option, $default );
		$value = get_option($option, $default);
	} else {
		$cache_key = "{$wpdb->siteid}:$option";
		if ( $use_cache )
			$value = wp_cache_get($cache_key, 'site-options');

		if ( !isset($value) || (false === $value) ) {
			$row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );

			// Has to be get_row instead of get_var because of funkiness with 0, false, null values
			if ( is_object( $row ) ) {
				$value = $row->meta_value;
				$value = maybe_unserialize( $value );
				wp_cache_set( $cache_key, $value, 'site-options' );
			} else {
				if ( ! is_array( $notoptions ) ) {
					 $notoptions = array();
				}
				$notoptions[$option] = true;
				wp_cache_set( $notoptions_key, $notoptions, 'site-options' );

				/** This filter is documented in wp-includes/option.php */
				$value = apply_filters( 'default_site_option_' . $option, $default );
			}
		}
	}

	/**
	 * Filter the value of an existing site option.
	 *
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
	 *
	 * @since 2.9.0 As 'site_option_' . $key
	 * @since 3.0.0
	 *
	 * @param mixed $value Value of site option.
	 */
 	return apply_filters( 'site_option_' . $option, $value );
}
更新版本 源码位置 使用 被使用
4.4.0 wp-includes/option.php:1153 69 1 function

笔记(Notes)

默认用法
传递默认值

get_site_option() 为WP2原创文章,链接:https://www.wp2.cn/functions/get_site_option/

发表评论

您的电子邮箱地址不会被公开。