Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

WP_Network::get_main_site_id() WordPress Method

The WP_Network::get_main_site_id() function returns the id of the main site for the network.

WP_Network::get_main_site_id() #

Returns the main site ID for the network.


Description

Internal method used by the magic getter for the ‘blog_id’ and ‘site_id’ properties.


Top ↑

Return

(int) The ID of the main site.


Top ↑

Source

File: wp-includes/class-wp-network.php

	private function get_main_site_id() {
		/**
		 * Filters the main site ID.
		 *
		 * Returning a positive integer will effectively short-circuit the function.
		 *
		 * @since 4.9.0
		 *
		 * @param int|null   $main_site_id If a positive integer is returned, it is interpreted as the main site ID.
		 * @param WP_Network $network      The network object for which the main site was detected.
		 */
		$main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this );
		if ( 0 < $main_site_id ) {
			return $main_site_id;
		}

		if ( 0 < (int) $this->blog_id ) {
			return (int) $this->blog_id;
		}

		if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path )
			|| ( defined( 'SITE_ID_CURRENT_SITE' ) && SITE_ID_CURRENT_SITE == $this->id ) ) {
			if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
				$this->blog_id = (string) BLOG_ID_CURRENT_SITE;

				return (int) $this->blog_id;
			}

			if ( defined( 'BLOGID_CURRENT_SITE' ) ) { // Deprecated.
				$this->blog_id = (string) BLOGID_CURRENT_SITE;

				return (int) $this->blog_id;
			}
		}

		$site = get_site();
		if ( $site->domain === $this->domain && $site->path === $this->path ) {
			$main_site_id = (int) $site->id;
		} else {
			$cache_key = 'network:' . $this->id . ':main_site';

			$main_site_id = wp_cache_get( $cache_key, 'site-options' );
			if ( false === $main_site_id ) {
				$_sites       = get_sites(
					array(
						'fields'     => 'ids',
						'number'     => 1,
						'domain'     => $this->domain,
						'path'       => $this->path,
						'network_id' => $this->id,
					)
				);
				$main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;

				wp_cache_add( $cache_key, $main_site_id, 'site-options' );
			}
		}

		$this->blog_id = (string) $main_site_id;

		return (int) $this->blog_id;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.