wppaste
WordPress

WP_Network::get_main_site_id(): int

Since
4.9.0
Source
wp-includes/class-wp-network.php:219
Returns the main site ID for the network.

Description

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

Compatibility

WordPress
since 4.9.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.

Return value

int
The ID of the main site.

Performance profile

How much work a call to WP_Network::get_main_site_id() 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 get_network_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
10–89

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

Plugin surface
1 hook

Third-party callbacks on 'pre_get_main_site_id' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place 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
  • optionnetwork optionget_network_option()called directly
  • cacheobject cachewp_cache_get()one call below WP_Network::get_main_site_id()
  • serializeserialisationmaybe_unserialize()one call below WP_Network::get_main_site_id()

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 · 5 distinct outcomes

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

WhenInstructionsCalls it makes
always10–43apply_filters()
!$main_site_id37–56apply_filters(), get_site()
!$main_site_id && $main_site_id !== false39–62apply_filters(), get_site(), get_network_option()
!$main_site_id && $main_site_id === false && empty($_sites)62–85apply_filters(), get_site(), get_network_option(), ids(), update_network_option()
!$main_site_id && $main_site_id === false && !empty($_sites)66–89apply_filters(), get_site(), get_network_option(), ids(), array_shift(), update_network_option()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11010–88141 fewer instruction than PHP 8.5
8.511110–8914
8.411110–8914
8.311110–8914
8.211110–8914
8.111110–8914
7.411110–8914

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 · 1

One hook fires while WP_Network::get_main_site_id() runs, in this order:

  1. apply_filters( pre_get_main_site_id )filterline 230 (+11 into the body)

    Filters the main site ID.

Uses · 5

Used by · 1

Source code

	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' ) && (int) 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 { 			$main_site_id = get_network_option( $this->id, 'main_site' );			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; 				update_network_option( $this->id, 'main_site', $main_site_id );			}		} 		$this->blog_id = (string) $main_site_id; 		return (int) $this->blog_id;	}

Changelog

Introduced in 4.9.0. Unchanged from 6.7.7 through 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.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/class-wp-network.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.