Using WordPress ‘get_main_site_id()’ PHP function

The get_main_site_id() WordPress PHP function retrieves the main site ID of a network.

Usage

To use the function, simply call it and pass the network ID as a parameter if needed. If no parameter is provided, it will return the main site ID of the current network.

$main_site_id = get_main_site_id($network_id);

Parameters

  • $network_id (int, optional) – The ID of the network for which to get the main site. Defaults to the current network. Default: null.

More information

See WordPress Developer Resources: get_main_site_id()

Examples

Get main site ID of current network

This example retrieves the main site ID of the current network.

$main_site_id = get_main_site_id();
echo 'The main site ID is: ' . $main_site_id;

Get main site ID of a specific network

In this example, we get the main site ID of a specific network by providing the network ID.

$network_id = 2;
$main_site_id = get_main_site_id($network_id);
echo 'The main site ID for network ' . $network_id . ' is: ' . $main_site_id;

Display main site URL

This example retrieves the main site ID and uses it to display the main site URL.

$main_site_id = get_main_site_id();
$main_site_url = get_site_url($main_site_id);
echo 'The main site URL is: ' . $main_site_url;

Get main site title

In this example, we get the main site title by first retrieving the main site ID.

$main_site_id = get_main_site_id();
$main_site_title = get_bloginfo('name', '', $main_site_id);
echo 'The main site title is: ' . $main_site_title;

Get main site theme

This example retrieves the main site ID and uses it to get the active theme of the main site.

$main_site_id = get_main_site_id();
$main_site_theme = get_blog_option($main_site_id, 'stylesheet');
echo 'The main site theme is: ' . $main_site_theme;