函数文档

get_site_option()

💡 云策文档标注

概述

get_site_option() 函数用于检索当前网络的选项值,基于选项名称。在 WordPress 多站点环境中,它返回网络范围的选项;对于非多站点安装,则使用 get_option()。该函数本质上是 get_network_option() 的包装器。

关键要点

  • 函数签名:get_site_option( $option, $default_value = false, $deprecated = true ),其中 $option 为必需参数(选项名称,无需 SQL 转义),$default_value 为可选默认值(默认为 false),$deprecated 为可选参数(仅多站点使用,始终设为 true)。
  • 返回值:返回选项的设置值,若选项不存在则返回 $default_value。
  • 与 get_option() 的区别:在多站点中,get_site_option() 检索网络级选项,而 get_option() 检索站点级选项;非多站点时两者行为相同。
  • 历史背景:由于多站点术语演变(如“blogs”变为“sites”),此函数可视为“get_network_option()”的别名,自 WordPress 4.4.0 起改为 get_network_option() 的包装器。
  • 相关函数:主要关联 get_network_option(),广泛用于网络管理、主题/插件控制、自动更新等核心功能。

代码示例

// 默认用法:检索选项值
echo get_site_option( 'siteurl' );

// 传递默认值:当选项不存在时返回指定值
$value = get_site_option( 'i_dont_exist' ); // $value == false
$value = get_site_option( 'i_dont_exist', 'blah' ); // $value == 'blah'

📄 原文内容

Retrieve an option value for the current network based on name of option.

Description

See also

Parameters

$optionstringrequired
Name of the option to retrieve. Expected to not be SQL-escaped.
$default_valuemixedoptional
Value to return if the option doesn’t exist.

Default:false

$deprecatedbooloptional
Whether to use cache. Multisite only. Always set to true.

Default:true

Return

mixed Value set for the option.

More Information

This function is almost identical to get_option(), except that in multisite, it returns the network-wide option. For non-multisite installs, it uses get_option.

It is easy to get confused about the difference between get_option() and get_site_option() , because multisite used different terms before. Now there are different “sites” on a “network”, before there where different “blogs” on a “site”. Many functions and variables were introduced before this change, such as this one. Think of this function as “get_network_option()

Source

function get_site_option( $option, $default_value = false, $deprecated = true ) {
	return get_network_option( null, $option, $default_value );
}

Changelog

Version Description
4.4.0 Modified into wrapper for get_network_option()
2.8.0 Introduced.

User Contributed Notes