get_network_option()WP 4.4.0

Gets the value of the specified network option (the main site in a multisite network).

If this function is called not in an MU build, control is passed to get_option().

The function retrieves data from the cache if possible.

When installing Multisite the option structure of sites is expanded.

In WordPress, there are four similar functions:

  1. get_network_option()
  2. get_site_option()
  3. get_blog_option()
  4. get_option()

These functions can be grouped:

get_network_option() equals get_site_option()
get_blog_option() equals get_option()

For more details, see: Site and Blog Structure.

Returns

Mixed. The option value if it was successfully retrieved. If the specified option does not exist, it will return false or the default value specified in the 3rd parameter.

Usage

get_network_option( $network_id, $option, $default );
$network_id(integer) (required)
ID of the network (main site, blog). You can specify null, in which case the ID of the current network will be used.
$option(string) (required)
The name of the option to retrieve. Expects a "dirty" string for the SQL query.
$default(mixed)
The value to return if the option does not exist.
Default: false

Examples

0

#1 Get the email of Administrator of the site network

$admin_email = get_network_option( null, 'admin_email' );

This line can be replaced with:

$admin_email = get_site_option( 'admin_email' );

List of some network site options:

Option Name Value
site_name Multisite
admin_email [email protected]
admin_user_id 1
registration user
upload_filetypes jpg jpeg png gif mov avi mpg 3gp 3g2 midi mid pdf doc ppt odt pptx docx pps ppsx xls xlsx key mp3 ogg wma m4a wav mp4 m4v webm ogv wmv flv
blog_upload_space 100
fileupload_maxk 1500
site_admins a:1:{i:0;s:4:"nick";}
allowedthemes a:1:{s:19:"twentysixteen-child";b:1;}
wpmu_upgrade_site 37965
welcome_email Hello, USERNAME! Your new site in the "SITE_NAME" network has been successfully created at: ....
first_post Welcome to the site %s. This is your first post. Edit or delete it, then write!
siteurl http://multiexample.com/wp/
add_new_users 1
upload_space_check_disabled 1
subdomain_install 1
global_terms_enabled 0
ms_files_rewriting 0
initial_db_version 36686
active_sitewide_plugins a:1:{s:33:"user-switching/user-switching.php";i:1473022930;}
WPLANG ru_RU
user_count 4
blog_count 3
can_compress_scripts 1
recently_activated a:1:{s:28:"democracy-poll/democracy.php";i:1473020642;}
registrationnotification yes
welcome_user_email Hello, USERNAME! Your account has been set up. ....
menu_items a:0:{}
first_page ''
first_comment ''
first_comment_url ''
first_comment_author ''
limited_email_domains ''
banned_email_domains ''
first_comment_email ''

Notes

  • See: get_option()
  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 4.4.0 Introduced.

get_network_option() code WP 6.9.1

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 );

	/**
	 * Filters the value of any existing network option before it is retrieved.
	 *
	 * Returning a value other than false from the filter will short-circuit retrieval
	 * and return that value instead.
	 *
	 * @since 6.9.0
	 *
	 * @param mixed  $pre_option    The value to return instead of the network 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        Name of the option.
	 * @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', $pre, $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;
				$value = maybe_unserialize( $value );
				wp_cache_set( $cache_key, $value, 'site-options' );
			} else {
				if ( ! is_array( $notoptions ) ) {
					$notoptions = array();
				}

				$notoptions[ $option ] = true;
				wp_cache_set( $notoptions_key, $notoptions, 'site-options' );

				/** This filter is documented in wp-includes/option.php */
				$value = apply_filters( 'default_site_option_' . $option, $default_value, $option, $network_id );
			}
		}
	}

	if ( ! is_array( $notoptions ) ) {
		$notoptions = array();
		wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
	}

	/**
	 * Filters the value of an existing network option.
	 *
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
	 *
	 * @since 2.9.0 As '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.
	 */
	return apply_filters( "site_option_{$option}", $value, $option, $network_id );
}