Using WordPress ‘is_main_site()’ PHP function

The is_main_site() WordPress PHP function determines whether a site is the main site of the current network.

Usage

To check if a site is the main site, use is_main_site($site_id, $network_id). The function returns true if the site is the main site, and false otherwise.

Parameters

  • $site_id int Optional. Site ID to test. Defaults to current site. Default: null.
  • $network_id int Optional. Network ID of the network to check for. Defaults to current network. Default: null.

More information

See WordPress Developer Resources: is_main_site()

Examples

Check if the current site is the main site

This code checks if the current site is the main site and displays a message if it is.

if (is_main_site()) {
  echo 'This is the main site!';
}

Check if a specific site is the main site

This code checks if the site with ID 3 is the main site.

$site_id = 3;
if (is_main_site($site_id)) {
  echo 'Site 3 is the main site!';
}

Check if a specific site is the main site of a specific network

This code checks if the site with ID 2 is the main site of the network with ID 1.

$site_id = 2;
$network_id = 1;
if (is_main_site($site_id, $network_id)) {
  echo 'Site 2 is the main site of network 1!';
}

Display a special message for the main site

This code displays a special message only on the main site of the network.

if (is_main_site()) {
  echo 'Welcome to the main site!';
} else {
  echo 'Welcome to our subsite!';
}

Check and store if the current site is the main site

This code checks if the current site is the main site and stores the result in a variable.

$is_main_site = is_main_site();

if ($is_main_site) {
  echo 'This is the main site!';
} else {
  echo 'This is not the main site.';
}