update_network_option( int|null $network_id, string $option, mixed $value ): bool
- Since
- 4.4.0
- Source
wp-includes/option.php:2385
Compatibility
- WordPress
- since 4.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$network_idint|null- ID of the network. Can be null to default to the current network ID.
$optionstring- Name of the option. Expected to not be SQL-escaped.
$valuemixed- Option value. Expected to not be SQL-escaped.
Return value
bool- True if the value was updated, false otherwise.
Performance profile
How much work a call to update_network_option() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 8–107
- Plugin surface
- 3 hooks
- Called by
- 7
Reads stored settings via update_network_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 123.
Third-party callbacks on 'pre_update_site_option_{$option}', 'update_site_option_{$option}', 'update_site_option' run inside this call, and their cost is not bounded by anything here.
7 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionnetwork option
update_network_option()this function does it - hookthird-party callbacks
apply_filters()called directly - serializeserialisation
maybe_serialize()called directly - cacheobject cache
wp_cache_get()called directly
Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 27 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost update_network_option() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$network_id | 8 | none |
$value === false | 28–30 | wp_protect_special_option(), get_network_option(), apply_filters() |
$value === false | 31–33 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters() |
$value !== false | 36–38 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize() |
$value !== false | 39–41 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize() |
$value !== false && $old_value === false | 43–45 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), add_network_option() |
$value !== false && $old_value === false | 46–48 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), add_network_option() |
$value !== false && $old_value !== false && !is_multisite() | 57–61 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), is_multisite(), update_option() |
$value !== false && $old_value !== false && !is_multisite() | 60–64 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), is_multisite(), update_option() |
$value !== false && $old_value !== false && is_array($notoptions) && isset($notoptions[$option]) && !is_multisite() | 65–67 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), wp_cache_set(), is_multisite(), update_option() |
$value !== false && $old_value !== false && is_array($notoptions) && isset($notoptions[$option]) && !is_multisite() | 68–70 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), wp_cache_set(), is_multisite(), update_option() |
$value !== false && $old_value !== false && is_multisite() | 70–74 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), is_multisite(), sanitize_option(), maybe_serialize(), site_id() |
15 further outcomes, up to 107 instructions
$value !== false && $old_value !== false && !is_multisite() | 72–76 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), is_multisite(), update_option(), do_action(), do_action() |
$value !== false && $old_value !== false && is_multisite() | 73–77 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), is_multisite(), sanitize_option(), maybe_serialize(), site_id() |
$value !== false && $old_value !== false && !is_multisite() | 75–79 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), is_multisite(), update_option(), do_action(), do_action() |
$value !== false && $old_value !== false && is_array($notoptions) && isset($notoptions[$option]) && is_multisite() | 78–80 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), wp_cache_set(), is_multisite(), sanitize_option(), maybe_serialize(), site_id() |
$value !== false && $old_value !== false && is_multisite() | 79–83 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), is_multisite(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set() |
$value !== false && $old_value !== false && is_array($notoptions) && isset($notoptions[$option]) && !is_multisite() | 80–82 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), wp_cache_set(), is_multisite(), update_option(), do_action(), do_action() |
$value !== false && $old_value !== false && is_array($notoptions) && isset($notoptions[$option]) && is_multisite() | 81–83 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), wp_cache_set(), is_multisite(), sanitize_option(), maybe_serialize(), site_id() |
$value !== false && $old_value !== false && is_multisite() | 82–86 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), is_multisite(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set() |
$value !== false && $old_value !== false && is_array($notoptions) && isset($notoptions[$option]) && !is_multisite() | 83–85 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), wp_cache_set(), is_multisite(), update_option(), do_action(), do_action() |
$value !== false && $old_value !== false && is_array($notoptions) && isset($notoptions[$option]) && is_multisite() | 87–89 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), wp_cache_set(), is_multisite(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set() |
$value !== false && $old_value !== false && is_array($notoptions) && isset($notoptions[$option]) && is_multisite() | 90–92 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), wp_cache_set(), is_multisite(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set() |
$value !== false && $old_value !== false && is_multisite() | 94–98 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), is_multisite(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), do_action(), do_action() |
$value !== false && $old_value !== false && is_multisite() | 97–101 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), is_multisite(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), do_action(), do_action() |
$value !== false && $old_value !== false && is_array($notoptions) && isset($notoptions[$option]) && is_multisite() | 102–104 | wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), wp_cache_set(), is_multisite(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), do_action(), do_action() |
$value !== false && $old_value !== false && is_array($notoptions) && isset($notoptions[$option]) && is_multisite() | 105–107 | get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), wp_cache_get(), wp_cache_set(), is_multisite(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), do_action(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 123 | 8–107 | 11 | |
| 8.5 | 123 | 8–107 | 11 | |
| 8.4 | 123 | 8–107 | 11 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 125 | 10–109 | 11 | |
| 8.2 | 125 | 10–109 | 11 | |
| 8.1 | 125 | 10–109 | 11 | |
| 7.4 | 125 | 10–109 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 3
3 hooks fire while update_network_option() runs, in this order:
- apply_filters( pre_update_site_option_{$option} )filterline 2418 (+33 into the body)
Filters a specific network option before its value is updated.
- do_action( update_site_option_{$option} )actionline 2482 (+97 into the body)
Fires after the value of a specific network option has been successfully updated.
- do_action( update_site_option )actionline 2495 (+110 into the body)
Fires after the value of a network option has been successfully updated.
Uses · 12
- get_current_network_id()Retrieves the current network ID.
- wp_protect_special_option()Protects WordPress special option from being modified.
- get_network_option()Retrieves a network's option value based on the option name.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- maybe_serialize()Serializes data, if needed.
- add_network_option()Adds a new network option.
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- wp_cache_set()Saves the data to the cache.
- is_multisite()Determines whether Multisite is enabled.
- update_option()Updates the value of an option that was already added.
- sanitize_option()Sanitizes various option values based on the nature of the option.
- do_action()Calls the callback functions that have been added to an action hook.
Used by · 7
- WP_Application_Passwords::create_new_application_password()Creates a new application password.
- WP_Network::get_main_site_id()Returns the main site ID for the network.
- is_site_meta_supported()Determines whether site meta is enabled.
- update_site_option()Updates the value of an option that was already added for the current network.
- upgrade_560()Executes changes made in WordPress 5.6.0.
- wp_update_network_site_counts()Updates the network-wide site count.
- wp_update_user_counts()Updates the total count of users on the site.
Source code
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' ); } }Changelog
Introduced in 4.4.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$network_id retyped from int to int|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/option.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.