get_network_option()
云策文档标注
概述
get_network_option() 函数用于根据选项名称检索网络选项的值,支持多站点环境。它通过参数指定网络ID、选项名称和默认值,并包含缓存机制和多个过滤器钩子以增强灵活性和性能。
关键要点
- 函数签名:get_network_option( $network_id, $option, $default_value = false ),返回混合类型的选项值。
- 参数说明:$network_id 可为整数或 null(默认为当前网络ID),$option 为字符串选项名(无需SQL转义),$default_value 为选项不存在时的默认返回值。
- 内部处理:包括网络ID验证、缓存检查(使用 wp_cache_get/set)、数据库查询(通过 $wpdb->get_row)和序列化处理(maybe_unserialize)。
- 过滤器钩子:提供 pre_site_option_{$option}、pre_site_option、default_site_option_{$option} 和 site_option_{$option} 等钩子,允许在检索前后修改值。
- 多站点支持:在非多站点环境中回退到 get_option,在多站点中从 $wpdb->sitemeta 表查询。
代码示例
// 示例:获取网络选项
$network_id = 1; // 指定网络ID
$option_name = 'site_name';
$default = '默认站点名';
$value = get_network_option( $network_id, $option_name, $default );
echo $value; // 输出选项值或默认值注意事项
- 函数自 WordPress 4.4.0 版本引入,适用于多站点环境,但也能在单站点中工作。
- 选项名称不应进行 SQL 转义,函数内部使用 wpdb::prepare 处理查询安全。
- 缓存键基于网络ID和选项名,避免重复查询,提高性能。
- 过滤器钩子如 pre_site_option 可用于短路检索,直接返回自定义值。
原文内容
Retrieves a network’s option value based on the option name.
Description
See also
Parameters
$network_idint|nullrequired-
ID of the network. Can be null to default to the current network ID.
$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
Source
function get_network_option( $network_id, $option, $default_value = false ) {
global $wpdb;
if ( $network_id && ! is_numeric( $network_id ) ) {
return false;
}
$network_id = (int) $network_id;
// Fallback to the current network if a network ID is not specified.
if ( ! $network_id ) {
$network_id = get_current_network_id();
}
/**
* Filters the value of an existing network option before it is retrieved.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* Returning a value other than false from the filter will short-circuit retrieval
* and return that value instead.
*
* @since 2.9.0 As 'pre_site_option_' . $key
* @since 3.0.0
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$network_id` parameter was added.
* @since 4.9.0 The `$default_value` parameter was added.
*
* @param mixed $pre_site_option The value to return instead of the option value. This differs from
* `$default_value`, which is used as the fallback value in the event
* the option doesn't exist elsewhere in get_network_option().
* Default false (to skip past the short-circuit).
* @param string $option Option name.
* @param int $network_id ID of the network.
* @param mixed $default_value The fallback value to return if the option does not exist.
* Default false.
*/
$pre = apply_filters( "pre_site_option_{$option}", false, $option, $network_id, $default_value );
/**
* Filters the value of any existing network option before it is retrieved.
*
* Returning a value other than false from the filter will short-circuit retrieval
* and return that value instead.
*
* @since 6.9.0
*
* @param mixed $pre_option The value to return instead of the network option value. This differs
* from `$default_value`, which is used as the fallback value in the event
* the option doesn't exist elsewhere in get_network_option().
* Default false (to skip past the short-circuit).
* @param string $option Name of the option.
* @param int $network_id ID of the network.
* @param mixed $default_value The fallback value to return if the option does not exist.
* Default false.
*/
$pre = apply_filters( 'pre_site_option', $pre, $option, $network_id, $default_value );
if ( false !== $pre ) {
return $pre;
}
// Prevent non-existent options from triggering multiple queries.
$notoptions_key = "$network_id:notoptions";
$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
/**
* Filters the value of a specific default network option.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* @since 3.4.0
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$network_id` parameter was added.
*
* @param mixed $default_value The value to return if the site option does not exist
* in the database.
* @param string $option Option name.
* @param int $network_id ID of the network.
*/
return apply_filters( "default_site_option_{$option}", $default_value, $option, $network_id );
}
if ( ! is_multisite() ) {
/** This filter is documented in wp-includes/option.php */
$default_value = apply_filters( 'default_site_option_' . $option, $default_value, $option, $network_id );
$value = get_option( $option, $default_value );
} else {
$cache_key = "$network_id:$option";
$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, $network_id ) );
// 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_value, $option, $network_id );
}
}
}
if ( ! is_array( $notoptions ) ) {
$notoptions = array();
wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
}
/**
* Filters the value of an existing network 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
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$network_id` parameter was added.
*
* @param mixed $value Value of network option.
* @param string $option Option name.
* @param int $network_id ID of the network.
*/
return apply_filters( "site_option_{$option}", $value, $option, $network_id );
}
Hooks
- apply_filters( “default_site_option_{$option}”, mixed $default_value, string $option, int $network_id )
-
Filters the value of a specific default network option.
- apply_filters( ‘pre_site_option’, mixed $pre_option, string $option, int $network_id, mixed $default_value )
-
Filters the value of any existing network option before it is retrieved.
- apply_filters( “pre_site_option_{$option}”, mixed $pre_site_option, string $option, int $network_id, mixed $default_value )
-
Filters the value of an existing network option before it is retrieved.
- apply_filters( “site_option_{$option}”, mixed $value, string $option, int $network_id )
-
Filters the value of an existing network option.
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |