wppaste
WordPress

update_network_option( int|null $network_id, string $option, mixed $value ): bool

Since
4.4.0
Source
wp-includes/option.php:2385
Updates the value of a network option that was already added.

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

Reads stored settings via update_network_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8–107

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 123.

Plugin surface
3 hooks

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.

Called by
7

7 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionnetwork optionupdate_network_option()this function does it
  • hookthird-party callbacksapply_filters()called directly
  • serializeserialisationmaybe_serialize()called directly
  • cacheobject cachewp_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.

WhenInstructionsCalls it makes
!$network_id8none
$value === false28–30wp_protect_special_option(), get_network_option(), apply_filters()
$value === false31–33get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters()
$value !== false36–38wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize()
$value !== false39–41get_current_network_id(), wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize()
$value !== false && $old_value === false43–45wp_protect_special_option(), get_network_option(), apply_filters(), maybe_serialize(), maybe_serialize(), add_network_option()
$value !== false && $old_value === false46–48get_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–61wp_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–64get_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–67wp_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–70get_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–74wp_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–76wp_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–77get_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–79get_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–80wp_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–83wp_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–82wp_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–83get_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–86get_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–85get_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–89wp_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–92get_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–98wp_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–101get_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–104wp_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–107get_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

PHPCompiledExecutedBranchesNotes
8.6-dev1238–10711
8.51238–10711
8.41238–107112 fewer instructions than PHP 8.3
8.312510–10911
8.212510–10911
8.112510–10911
7.412510–10911

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:

  1. apply_filters( pre_update_site_option_{$option} )filterline 2418 (+33 into the body)

    Filters a specific network option before its value is updated.

  2. 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.

  3. 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

Used by · 7

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.8.8
Parameter $network_id retyped from int to int|null.verified against source
4.4.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.