On this page
update_network_option( int $network_id, string $option, mixed $value ): bool
Updates the value of a network option that was already added.
Description
See also
Parameters
$network_idint Required-
ID of the network. Can be null to default to the current network ID.
$optionstring Required-
Name of the option. Expected to not be SQL-escaped.
$valuemixed Required-
Option value. Expected to not be SQL-escaped.
Return
bool True if the value was updated, false otherwise.
Source
File: wp-includes/option.php. View all references
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, false );
/**
* 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, 'no' );
} 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.
Related
Uses
| Uses | Description |
|---|---|
| get_current_network_id() wp-includes/load.php | Retrieves the current network ID. |
| add_network_option() wp-includes/option.php | Adds a new network option. |
| get_network_option() wp-includes/option.php | Retrieves a network’s option value based on the option name. |
| wp_cache_set() wp-includes/cache.php | Saves the data to the cache. |
| sanitize_option() wp-includes/formatting.php | Sanitizes various option values based on the nature of the option. |
| maybe_serialize() wp-includes/functions.php | Serializes data, if needed. |
| wp_protect_special_option() wp-includes/option.php | Protects WordPress special option from being modified. |
| wpdb::update() wp-includes/class-wpdb.php | Updates a row in the table. |
| wp_cache_get() wp-includes/cache.php | Retrieves the cache contents from the cache by key and group. |
| is_multisite() wp-includes/load.php | If Multisite is enabled. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| update_option() wp-includes/option.php | Updates the value of an option that was already added. |
Used By
| Used By | Description |
|---|---|
| wp_update_user_counts() wp-includes/user.php | Updates the total count of users on the site. |
| WP_Application_Passwords::create_new_application_password() wp-includes/class-wp-application-passwords.php | Creates a new application password. |
| is_site_meta_supported() wp-includes/functions.php | Determines whether site meta is enabled. |
| WP_Network::get_main_site_id() wp-includes/class-wp-network.php | Returns the main site ID for the network. |
| update_site_option() wp-includes/option.php | Updates the value of an option that was already added for the current network. |
| wp_update_network_site_counts() wp-includes/ms-functions.php | Updates the network-wide site count. |
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/update_network_option