wppaste
WordPress

get_network_option( int|null $network_id, string $option, mixed $default_value = false ): mixed

Since
4.4.0
Source
wp-includes/option.php:2001
Retrieves a network's option value based on the option name.

Parameters

$network_idint|null
ID of the network. Can be null to default to the current network ID.
$optionstring
Name of the option to retrieve. Expected to not be SQL-escaped.
$default_valuemixedoptional
Value to return if the option doesn't exist. Default false.Default: false

Return

mixed
Value set for the option.

Cost profile

Measured from the compiled opcodes, not from a stopwatch. Every number here is identical on any machine that runs the same PHP version, and every function in core is ranked by these figures.

Cost class
Heavy

Reaches the database via ->get_row().

Scaling
Constant

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

Instructions
8–98

Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 134.

Plugin surface
3 hooks

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

Called by
12

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

What it touches

  • optionnetwork optionget_network_option()this function does it
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()called directly
  • serializeserialisationmaybe_unserialize()called directly
  • sqldatabase query->get_row()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 · 21 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_network_option() can have, taken from its control-flow graph on PHP 8.4.

WhenInstructionsCalls it makes
!$network_id8none
$pre !== false20–22apply_filters()
$pre !== false23–25get_current_network_id()apply_filters()
$pre === false && is_array($notoptions) && isset($notoptions[$option])37–39apply_filters()wp_cache_get()apply_filters()
$pre === false && is_array($notoptions) && isset($notoptions[$option])40–42get_current_network_id()apply_filters()wp_cache_get()apply_filters()
$pre === false && is_multisite() && isset($value) && $value !== false && is_array($notoptions)52–56apply_filters()wp_cache_get()is_multisite()wp_cache_get()apply_filters()
$pre === false && !is_multisite() && is_array($notoptions)54–58apply_filters()wp_cache_get()is_multisite()apply_filters()get_option()apply_filters()
$pre === false && is_multisite() && isset($value) && $value !== false && is_array($notoptions)55–59get_current_network_id()apply_filters()wp_cache_get()is_multisite()wp_cache_get()apply_filters()
$pre === false && !is_multisite() && is_array($notoptions)57–61get_current_network_id()apply_filters()wp_cache_get()is_multisite()apply_filters()get_option()apply_filters()
$pre === false && !is_array($notoptions) && is_multisite() && isset($value) && $value !== false58–62apply_filters()wp_cache_get()is_multisite()wp_cache_get()wp_cache_set()apply_filters()
$pre === false && !is_array($notoptions) && !is_multisite()60–64apply_filters()wp_cache_get()is_multisite()apply_filters()get_option()wp_cache_set()apply_filters()
$pre === false && !is_array($notoptions) && is_multisite() && isset($value) && $value !== false61–65get_current_network_id()apply_filters()wp_cache_get()is_multisite()wp_cache_get()wp_cache_set()apply_filters()
9 further outcomes, up to 98 instructions
$pre === false && !is_array($notoptions) && !is_multisite()63–67get_current_network_id()apply_filters()wp_cache_get()is_multisite()apply_filters()get_option()wp_cache_set()apply_filters()
$pre === false && is_multisite() && is_object($row) && is_array($notoptions)77–83apply_filters()wp_cache_get()is_multisite()wp_cache_get()->prepare()->get_row()maybe_unserialize()wp_cache_set()apply_filters()
$pre === false && is_multisite() && is_object($row) && is_array($notoptions)80–86get_current_network_id()apply_filters()wp_cache_get()is_multisite()wp_cache_get()->prepare()->get_row()maybe_unserialize()wp_cache_set()apply_filters()
$pre === false && is_multisite() && !is_object($row) && is_array($notoptions)82–89apply_filters()wp_cache_get()is_multisite()wp_cache_get()->prepare()->get_row()wp_cache_set()apply_filters()apply_filters()
$pre === false && !is_array($notoptions) && is_multisite() && is_object($row)83–89apply_filters()wp_cache_get()is_multisite()wp_cache_get()->prepare()->get_row()maybe_unserialize()wp_cache_set()wp_cache_set()apply_filters()
$pre === false && is_multisite() && !is_object($row) && is_array($notoptions)85–92get_current_network_id()apply_filters()wp_cache_get()is_multisite()wp_cache_get()->prepare()->get_row()wp_cache_set()apply_filters()apply_filters()
$pre === false && !is_array($notoptions) && is_multisite() && is_object($row)86–92get_current_network_id()apply_filters()wp_cache_get()is_multisite()wp_cache_get()->prepare()->get_row()maybe_unserialize()wp_cache_set()wp_cache_set()apply_filters()
$pre === false && !is_array($notoptions) && is_multisite() && !is_object($row)88–95apply_filters()wp_cache_get()is_multisite()wp_cache_get()->prepare()->get_row()wp_cache_set()apply_filters()wp_cache_set()apply_filters()
$pre === false && !is_array($notoptions) && is_multisite() && !is_object($row)91–98get_current_network_id()apply_filters()wp_cache_get()is_multisite()wp_cache_get()->prepare()->get_row()wp_cache_set()apply_filters()wp_cache_set()apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
7.413610–10012More instructions than PHP 8.4
8.113610–10012More instructions than PHP 8.4
8.213610–10012More instructions than PHP 8.4
8.313610–10012More instructions than PHP 8.4
8.41348–9812Compiles to the same instructions

Oldest PHP that compiles this body: 7.4. An instruction is not a fixed amount of time, so a version with the same count is not necessarily the same speed; what the count rules out is a difference in the work itself.

Hooks fired · 5

5 hooks fire while get_network_option() runs, in this order:

  1. apply_filters( pre_site_option_{$option} )filterline 2038 (+37 into the body)

    Filters the value of an existing network option before it is retrieved.

  2. apply_filters( default_site_option_{$option} )filterline 2064 (+63 into the body)

    Filters the value of a specific default network option.

  3. apply_filters( default_site_option_{$option} )filterline 2069 (+68 into the body)

    Filters the value of a specific default network option.

  4. apply_filters( default_site_option_{$option} )filterline 2092 (+91 into the body)

    Filters the value of a specific default network option.

  5. apply_filters( site_option_{$option} )filterline 2116 (+115 into the body)

    Filters the value of an existing network option.

Uses · 7

Used by · 12

Source

function get_network_option( $network_id, $option, $default_value = false ) {	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();	} 	/**	 * Filters the value of an existing network option before it is retrieved.	 *	 * The dynamic portion of the hook name, `$option`, refers to the option name.	 *	 * Returning a value other than false from the filter will short-circuit retrieval	 * and return that value instead.	 *	 * @since 2.9.0 As 'pre_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.	 * @since 4.9.0 The `$default_value` parameter was added.	 *	 * @param mixed  $pre_site_option The value to return instead of the option value. This differs from	 *                                `$default_value`, which is used as the fallback value in the event	 *                                the option doesn't exist elsewhere in get_network_option().	 *                                Default false (to skip past the short-circuit).	 * @param string $option          Option name.	 * @param int    $network_id      ID of the network.	 * @param mixed  $default_value   The fallback value to return if the option does not exist.	 *                                Default false.	 */	$pre = apply_filters( "pre_site_option_{$option}", false, $option, $network_id, $default_value ); 	if ( false !== $pre ) {		return $pre;	} 	// Prevent non-existent options from triggering multiple queries.	$notoptions_key = "$network_id:notoptions";	$notoptions     = wp_cache_get( $notoptions_key, 'site-options' ); 	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { 		/**		 * Filters the value of a specific default network option.		 *		 * The dynamic portion of the hook name, `$option`, refers to the option name.		 *		 * @since 3.4.0		 * @since 4.4.0 The `$option` parameter was added.		 * @since 4.7.0 The `$network_id` parameter was added.		 *		 * @param mixed  $default_value The value to return if the site option does not exist		 *                              in the database.		 * @param string $option        Option name.		 * @param int    $network_id    ID of the network.		 */		return apply_filters( "default_site_option_{$option}", $default_value, $option, $network_id );	} 	if ( ! is_multisite() ) {		/** This filter is documented in wp-includes/option.php */		$default_value = apply_filters( 'default_site_option_' . $option, $default_value, $option, $network_id );		$value         = get_option( $option, $default_value );	} else {		$cache_key = "$network_id:$option";		$value     = wp_cache_get( $cache_key, 'site-options' ); 		if ( ! isset( $value ) || false === $value ) {			$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); 			// Has to be get_row() instead of get_var() because of funkiness with 0, false, null values.			if ( is_object( $row ) ) {				$value = $row->meta_value;

History

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