update_network_option()
云策文档标注
概述
update_network_option() 函数用于更新已添加的网络选项值,适用于 WordPress 多站点环境。它处理网络 ID、选项名称和值,并包含缓存管理、序列化比较和钩子触发等机制。
关键要点
- 函数签名:update_network_option( $network_id, $option, $value ),返回布尔值表示更新是否成功
- 参数:$network_id(整数或 null,默认为当前网络 ID)、$option(字符串,选项名称)、$value(混合类型,选项值)
- 内部逻辑:检查网络 ID 有效性,使用 get_current_network_id() 处理默认值,通过 wp_protect_special_option() 保护特殊选项
- 更新前通过 apply_filters( "pre_update_site_option_{$option}" ) 过滤值,比较新旧值以避免不必要更新
- 如果旧值不存在,则调用 add_network_option() 添加新选项;否则更新数据库并管理缓存
- 支持单站点和多站点环境:单站点时调用 update_option(),多站点时使用 wpdb::update() 操作 sitemeta 表
- 成功更新后触发 do_action( "update_site_option_{$option}" ) 和 do_action( 'update_site_option' ) 钩子
- 相关函数包括 get_network_option()、add_network_option()、sanitize_option()、maybe_serialize() 等
代码示例
// 更新网络选项示例
$network_id = 1; // 指定网络 ID,或使用 null 表示当前网络
$option_name = 'site_description';
$new_value = '新的网络描述';
$result = update_network_option( $network_id, $option_name, $new_value );
if ( $result ) {
echo '选项更新成功';
} else {
echo '选项更新失败或值未改变';
}注意事项
- 选项名称和值不应进行 SQL 转义,函数内部会处理
- 在非多站点环境中,此函数行为类似于 update_option(),但参数结构保持一致
- 使用前应确保选项已存在,否则可能触发 add_network_option()
- 注意钩子的动态部分,如 pre_update_site_option_{$option},可用于特定选项的定制过滤
原文内容
Updates the value of a network option that was already added.
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. Expected to not be SQL-escaped.
$valuemixedrequired-
Option value. Expected to not be SQL-escaped.
Source
function update_network_option( $network_id, $option, $value ) {
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();
}
wp_protect_special_option( $option );
$old_value = get_network_option( $network_id, $option );
/**
* Filters a specific network option before its value is updated.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* @since 2.9.0 As 'pre_update_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 New value of the network option.
* @param mixed $old_value Old value of the network option.
* @param string $option Option name.
* @param int $network_id ID of the network.
*/
$value = apply_filters( "pre_update_site_option_{$option}", $value, $old_value, $option, $network_id );
/*
* If the new and old values are the same, no need to update.
*
* Unserialized values will be adequate in most cases. If the unserialized
* data differs, the (maybe) serialized data is checked to avoid
* unnecessary database calls for otherwise identical object instances.
*
* See https://core.trac.wordpress.org/ticket/44956
*/
if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
return false;
}
if ( false === $old_value ) {
return add_network_option( $network_id, $option, $value );
}
$notoptions_key = "$network_id:notoptions";
$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
unset( $notoptions[ $option ] );
wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
}
if ( ! is_multisite() ) {
$result = update_option( $option, $value, false );
} else {
$value = sanitize_option( $option, $value );
$serialized_value = maybe_serialize( $value );
$result = $wpdb->update(
$wpdb->sitemeta,
array( 'meta_value' => $serialized_value ),
array(
'site_id' => $network_id,
'meta_key' => $option,
)
);
if ( $result ) {
$cache_key = "$network_id:$option";
wp_cache_set( $cache_key, $value, 'site-options' );
}
}
if ( $result ) {
/**
* Fires after the value of a specific network option has been successfully updated.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* @since 2.9.0 As "update_site_option_{$key}"
* @since 3.0.0
* @since 4.7.0 The `$network_id` parameter was added.
*
* @param string $option Name of the network option.
* @param mixed $value Current value of the network option.
* @param mixed $old_value Old value of the network option.
* @param int $network_id ID of the network.
*/
do_action( "update_site_option_{$option}", $option, $value, $old_value, $network_id );
/**
* Fires after the value of a network option has been successfully updated.
*
* @since 3.0.0
* @since 4.7.0 The `$network_id` parameter was added.
*
* @param string $option Name of the network option.
* @param mixed $value Current value of the network option.
* @param mixed $old_value Old value of the network option.
* @param int $network_id ID of the network.
*/
do_action( 'update_site_option', $option, $value, $old_value, $network_id );
return true;
}
return false;
}
Hooks
- apply_filters( “pre_update_site_option_{$option}”, mixed $value, mixed $old_value, string $option, int $network_id )
-
Filters a specific network option before its value is updated.
- do_action( ‘update_site_option’, string $option, mixed $value, mixed $old_value, int $network_id )
-
Fires after the value of a network option has been successfully updated.
- do_action( “update_site_option_{$option}”, string $option, mixed $value, mixed $old_value, int $network_id )
-
Fires after the value of a specific network option has been successfully updated.
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |