wppaste
WordPress

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

Since
4.4.0
Source
wp-includes/option.php:2152
Adds a new network option.

Description

Existing options will not be updated.

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 to add. Expected to not be SQL-escaped.
$valuemixed
Option value, can be anything. Expected to not be SQL-escaped.

Return value

bool
True if the option was added, false otherwise.

Performance profile

How much work a call to add_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 add_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–102

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

Plugin surface
3 hooks

Third-party callbacks on 'pre_add_site_option_{$option}', 'add_site_option_{$option}', 'add_site_option' run inside this call, and their cost is not bounded by anything here.

Called by
2

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

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeadd_option()called directly
  • cacheobject cachewp_cache_get()called directly
  • serializeserialisationmaybe_serialize()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 add_network_option() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$network_id8none
!is_multisite()33–35wp_protect_special_option(), apply_filters(), is_multisite(), add_option()
!is_multisite()36–38get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), add_option()
is_multisite()41–45wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option()
is_multisite()44–48get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option()
!is_multisite()46–48wp_protect_special_option(), apply_filters(), is_multisite(), add_option(), do_action(), do_action()
!is_multisite()49–51get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), add_option(), do_action(), do_action()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])56–58wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), sanitize_option(), maybe_serialize(), site_id()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])59–61get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), sanitize_option(), maybe_serialize(), site_id()
is_multisite()61–65wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option(), sanitize_option(), maybe_serialize(), site_id()
is_multisite()64–68get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option(), sanitize_option(), maybe_serialize(), site_id()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])69–73wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get()
15 further outcomes, up to 102 instructions
is_multisite() && is_array($notoptions) && isset($notoptions[$option])72–76get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get()
is_multisite()74–80wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])77–79wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), wp_cache_set()
is_multisite()77–83get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])80–82get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), wp_cache_set()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])82–86wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), wp_cache_set()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])82–86wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])85–89get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), wp_cache_set()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])85–89get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
is_multisite()87–93wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])90–92wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
is_multisite()90–96get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), do_action(), do_action()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])93–95get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])95–99wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()
is_multisite() && is_array($notoptions) && isset($notoptions[$option])98–102get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option(), sanitize_option(), maybe_serialize(), site_id(), wp_cache_set(), wp_cache_get(), wp_cache_set(), do_action(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1148–10211
8.51148–10211
8.41148–102112 fewer instructions than PHP 8.3
8.311610–10411
8.211610–10411
8.111610–10411
7.411610–10411

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 add_network_option() runs, in this order:

  1. apply_filters( pre_add_site_option_{$option} )filterline 2182 (+30 into the body)

    Filters the value of a specific network option before it is added.

  2. do_action( add_site_option_{$option} )actionline 2245 (+93 into the body)

    Fires after a specific network option has been successfully added.

  3. do_action( add_site_option )actionline 2257 (+105 into the body)

    Fires after a network option has been successfully added.

Uses · 11

Used by · 2

Source code

function add_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 ); 	/**	 * Filters the value of a specific network option before it is added.	 *	 * The dynamic portion of the hook name, `$option`, refers to the option name.	 *	 * @since 2.9.0 As `pre_add_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.	 */	$value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id ); 	$notoptions_key = "$network_id:notoptions"; 	if ( ! is_multisite() ) {		$result = add_option( $option, $value, '', false );	} else {		$cache_key = "$network_id:$option"; 		/*		 * Make sure the option doesn't already exist.		 * We can check the 'notoptions' cache before we ask for a DB query.		 */		$notoptions = wp_cache_get( $notoptions_key, 'site-options' ); 		if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {			if ( false !== get_network_option( $network_id, $option, false ) ) {				return false;			}		} 		$value = sanitize_option( $option, $value ); 		$serialized_value = maybe_serialize( $value );		$result           = $wpdb->insert(			$wpdb->sitemeta,			array(				'site_id'    => $network_id,				'meta_key'   => $option,				'meta_value' => $serialized_value,			)		); 		if ( ! $result ) {			return false;		} 		wp_cache_set( $cache_key, $value, 'site-options' ); 		// This option exists now.		$notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // Yes, again... we need it to be fresh. 		if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {			unset( $notoptions[ $option ] );			wp_cache_set( $notoptions_key, $notoptions, 'site-options' );		}	} 	if ( $result ) {

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