is_main_site()
Determines whether the current/specified site is the main site of the network (MU build).
Used in the WordPress multisite installation (MU). In a single-site installation it always returns true.
Works at an early stage of WordPress loading, even before the constant SHORTINIT.
Uses the global variable $current_site
1 time — 0.000013 sec (very fast) | 50000 times — 0.045192 sec (speed of light)
No Hooks.
Returns
true|false. Will return true if $site_id is the main site of the network or MU is not used. In other cases will return false.
Usage
is_main_site( $site_id );
- $site_id(int)
- Site ID to check.
Default: null (current site) - $network_id(int)
- Network ID whose main site is being checked.
Default: null (current network)
Examples
#1 Do anything only if this is the main site of the network
if ( is_main_site() ) {
// this is the main network site
}
Changelog
| Since 3.0.0 | Introduced. |
| Since 4.9.0 | The $network_id parameter was added. |
is_main_site() is main site code WP 6.9
function is_main_site( $site_id = null, $network_id = null ) {
if ( ! is_multisite() ) {
return true;
}
if ( ! $site_id ) {
$site_id = get_current_blog_id();
}
$site_id = (int) $site_id;
return get_main_site_id( $network_id ) === $site_id;
}