add_network_option( int|null $network_id, string $option, mixed $value ): bool
- Since
- 4.4.0
- Source
wp-includes/option.php:2154
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
- Scaling
- Constant
- Instructions
- 8–102
- Plugin surface
- 3 hooks
- Called by
- 2
Reads stored settings via add_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 114.
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.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
add_option()called directly - cacheobject cache
wp_cache_get()called directly - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$network_id | 8 | none |
!is_multisite() | 33–35 | wp_protect_special_option(), apply_filters(), is_multisite(), add_option() |
!is_multisite() | 36–38 | get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), add_option() |
is_multisite() | 41–45 | wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option() |
is_multisite() | 44–48 | get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option() |
!is_multisite() | 46–48 | wp_protect_special_option(), apply_filters(), is_multisite(), add_option(), do_action(), do_action() |
!is_multisite() | 49–51 | get_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–58 | wp_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–61 | get_current_network_id(), wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), sanitize_option(), maybe_serialize(), site_id() |
is_multisite() | 61–65 | wp_protect_special_option(), apply_filters(), is_multisite(), wp_cache_get(), get_network_option(), sanitize_option(), maybe_serialize(), site_id() |
is_multisite() | 64–68 | get_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–73 | wp_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–76 | get_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–80 | 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]) | 77–79 | 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() | 77–83 | get_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–82 | get_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–86 | 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]) | 82–86 | 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() && is_array($notoptions) && isset($notoptions[$option]) | 85–89 | get_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–89 | get_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–93 | 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]) | 90–92 | 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() | 90–96 | get_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–95 | get_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–99 | 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() |
is_multisite() && is_array($notoptions) && isset($notoptions[$option]) | 98–102 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 114 | 8–102 | 11 | |
| 8.5 | 114 | 8–102 | 11 | |
| 8.4 | 114 | 8–102 | 11 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 116 | 10–104 | 11 | |
| 8.2 | 116 | 10–104 | 11 | |
| 8.1 | 116 | 10–104 | 11 | |
| 7.4 | 116 | 10–104 | 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 add_network_option() runs, in this order:
- apply_filters( pre_add_site_option_{$option} )filterline 2184 (+30 into the body)
Filters the value of a specific network option before it is added.
- do_action( add_site_option_{$option} )actionline 2247 (+93 into the body)
Fires after a specific network option has been successfully added.
- do_action( add_site_option )actionline 2259 (+105 into the body)
Fires after a network option has been successfully added.
Uses · 11
- get_current_network_id()Retrieves the current network ID.
- wp_protect_special_option()Protects WordPress special option from being modified.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_multisite()Determines whether Multisite is enabled.
- add_option()Adds a new option.
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- get_network_option()Retrieves a network's option value based on the option name.
- sanitize_option()Sanitizes various option values based on the nature of the option.
- maybe_serialize()Serializes data, if needed.
- wp_cache_set()Saves the data to the cache.
- do_action()Calls the callback functions that have been added to an action hook.
Used by · 2
- add_site_option()Adds a new option for the current network.
- update_network_option()Updates the value of a network option that was already added.
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.
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 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.