WP_Network::get_main_site_id(): int
- Since
- 4.9.0
- Source
wp-includes/class-wp-network.php:219
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
- Scaling
- Constant
- Instructions
- 10–89
- Plugin surface
- 1 hook
- Called by
- 1
Reads stored settings via get_network_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 111.
Third-party callbacks on 'pre_get_main_site_id' run inside this call, and their cost is not bounded by anything here.
1 place 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 - optionnetwork option
get_network_option()called directly - cacheobject cache
wp_cache_get()one call below WP_Network::get_main_site_id() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10–43 | apply_filters() |
!$main_site_id | 37–56 | apply_filters(), get_site() |
!$main_site_id && $main_site_id !== false | 39–62 | apply_filters(), get_site(), get_network_option() |
!$main_site_id && $main_site_id === false && empty($_sites) | 62–85 | apply_filters(), get_site(), get_network_option(), ids(), update_network_option() |
!$main_site_id && $main_site_id === false && !empty($_sites) | 66–89 | apply_filters(), get_site(), get_network_option(), ids(), array_shift(), update_network_option() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 110 | 10–88 | 14 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 111 | 10–89 | 14 | |
| 8.4 | 111 | 10–89 | 14 | |
| 8.3 | 111 | 10–89 | 14 | |
| 8.2 | 111 | 10–89 | 14 | |
| 8.1 | 111 | 10–89 | 14 | |
| 7.4 | 111 | 10–89 | 14 |
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:
Uses · 5
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_site()Retrieves site data given a site ID or site object.
- get_network_option()Retrieves a network's option value based on the option name.
- get_sites()Retrieves a list of sites matching requested arguments.
- update_network_option()Updates the value of a network option that was already added.
Used by · 1
- WP_Network::__get()Getter.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.