Using is_main_site to determine main WordPress site

The is_main_site function in WordPress is a tool that checks whether a specific site is the main site in a WordPress Multisite network. A WordPress Multisite network allows a single WordPress installation to host multiple websites. Each of these websites can have its own content, users and settings, but they all share the same WordPress installation.

The is_main_site function is used to identify the primary site in this network. This can be useful in various scenarios, such as when you want to apply certain settings or display specific content only on the main site.

It’s important to note that this function only works in a Multisite context. If WordPress is not set up in Multisite mode, the function will always return true, because in a single-site setup, the one site is by definition the main site.

Parameters Accepted by the is_main_site Function

The is_main_site function in WordPress accepts two parameters, both of which are optional:

  • $site_id (int): This is an optional parameter and its default value is null. This parameter represents the ID of the site that you want to test. If it’s not specified, the function will default to the current site.
  • $network_id (int): This is also an optional parameter and its default value is null. It represents the ID of the network that you want to check. If it’s not provided, the function will default to the current network.

If the function doesn’t accept any parameters, it will be explicitly stated in a short, clear sentence.

Return Value of the is_main_site Function

The is_main_site function returns a boolean value. It will return true if the $site_id is the main site of the network, or if WordPress is not running in Multisite mode.

Examples

How to check if the current site is the main site in a WordPress Multisite Network

if (is_main_site()) {
 echo '<p>This is the main site.</p>';
} else {
 echo '<p>This is not the main site.</p>';
}

This code snippet checks if the current site is the main site in a WordPress Multisite Network. If the current site is the main site, it echoes “This is the main site.”. If the current site is not the main site, it echoes “This is not the main site.”.

How to run a function only on the main site in a WordPress Multisite Network

function run_on_main_site() {
 if (is_main_site()) {
 // Your function code here
 }
}

This code snippet will only run the function run_on_main_site() if the current site is the main site in a WordPress Multisite Network. The function code should be placed where the comment is.

How to add a menu item only on the main site in a WordPress Multisite Network

function add_menu_item() {
 if (is_main_site()) {
 add_menu_page('Page Title', 'Menu Title', 'manage_options', 'menu_slug', 'function_to_handle_the_page', 'dashicons-admin-site', 6);
 }
}
add_action('admin_menu', 'add_menu_item');

This code snippet adds a menu item to the WordPress admin dashboard only if the current site is the main site in a WordPress Multisite Network. The function add_menu_item() is hooked to the admin_menu action hook, which is triggered when the admin menu is rendered. The add_menu_page() function is used to add a new menu page to the dashboard.

Conclusion

The is_main_site function is a feature that checks if the current site being viewed is the main site in a network of WordPress sites. This function is typically utilized in multisite environments, where there are multiple websites operating under a single WordPress installation. It can be used to apply certain changes or features specifically to the main site of the network, or to differentiate the main site from the other sites in the network.

Related WordPress Functions