Using WordPress ‘get_main_network_id()’ PHP function

The get_main_network_id() WordPress PHP function retrieves the main network ID.

Usage

$main_network_id = get_main_network_id();

Parameters

  • None

More information

See WordPress Developer Resources: get_main_network_id()

Examples

Display Main Network ID

This example shows how to display the main network ID in a multisite environment.

$main_network_id = get_main_network_id();
echo 'The main network ID is: ' . $main_network_id;

Get Main Network’s Site URL

This example retrieves the main network’s site URL using the main network ID.

$main_network_id = get_main_network_id();
$main_site_url = get_network_site_url($main_network_id);
echo 'The main network site URL is: ' . $main_site_url;

Check If Current Network Is the Main Network

This example checks if the current network is the main network and displays a message accordingly.

$main_network_id = get_main_network_id();
$current_network_id = get_current_network_id();

if ($main_network_id === $current_network_id) {
    echo 'This is the main network.';
} else {
    echo 'This is not the main network.';
}

Get Main Network’s Blog Count

This example retrieves the number of blogs in the main network.

$main_network_id = get_main_network_id();
$blog_count = get_blog_count($main_network_id);
echo 'The main network has ' . $blog_count . ' blogs.';

Switch to the Main Network’s Site

This example switches to the main network’s site and retrieves its title.

$main_network_id = get_main_network_id();
$main_site_id = get_network($main_network_id)->site_id;
switch_to_blog($main_site_id);
$main_site_title = get_bloginfo('name');
restore_current_blog();
echo 'The main network site title is: ' . $main_site_title;