Using WordPress ‘get_current_network_id()’ PHP function

The get_current_network_id() WordPress PHP function retrieves the current network ID.

Usage

$current_network_id = get_current_network_id();

Parameters

  • None

More information

See WordPress Developer Resources: get_current_network_id()

Examples

Display the current network ID

This example will display the current network ID.

$current_network_id = get_current_network_id();
echo 'Current Network ID: ' . $current_network_id;

Check if on the main network

This example will check if the current site is on the main network.

$current_network_id = get_current_network_id();
if ($current_network_id == 1) {
    echo 'On the main network';
} else {
    echo 'Not on the main network';
}

Create a function to get the network name

This example will create a function to get the network name based on the network ID.

function get_network_name($network_id) {
    // Get the network details
    $network = get_network($network_id);

    // Return the network name
    return $network->site_name;
}

$current_network_id = get_current_network_id();
echo 'Current Network Name: ' . get_network_name($current_network_id);

Display a list of sites in the current network

This example will display a list of sites within the current network.

$current_network_id = get_current_network_id();
$args = array('network_id' => $current_network_id);
$sites = get_sites($args);

echo 'Sites in the current network:';
echo '<ul>';
foreach ($sites as $site) {
    echo '<li>' . $site->blogname . '</li>';
}
echo '</ul>';

Get the current network’s main site

This example will get the main site for the current network.

$current_network_id = get_current_network_id();
$network_main_site_id = get_network($current_network_id)->blog_id;

echo 'Main site for current network: ' . get_blog_details($network_main_site_id)->blogname;