Using WordPress ‘get_current_site()’ PHP function

The get_current_site() WordPress PHP function returns an object containing the ‘id’, ‘domain’, ‘path’, and ‘site_name’ properties of the network being viewed.

Usage

$current_site = get_current_site();

Parameters

  • None

More information

See WordPress Developer Resources: get_current_site()

Note: The function was inherited from MU and is named based on the old terminology, which spoke of multiple “blogs” on a “site”. Nowadays, we speak of multiple “sites” on a “network”. This function returns information about the current network. To get information about the current site on the network, see get_current_blog_id() and get_blog_details().

Examples

Display network information

This example displays the network information (ID, domain, path, and site name) using the get_current_site() function.

$current_site = get_current_site();

echo "Network ID: " . $current_site->id . "<br>";
echo "Domain: " . $current_site->domain . "<br>";
echo "Path: " . $current_site->path . "<br>";
echo "Site Name: " . $current_site->site_name . "<br>";

Check if the current network is the main network

This example checks if the current network is the main network.

$current_site = get_current_site();

if ($current_site->id == 1) {
    echo "You are on the main network.";
} else {
    echo "You are on a secondary network.";
}

Display a message if a specific domain is the current network

This example checks if the current network domain is “example.com” and displays a message if it is.

$current_site = get_current_site();

if ($current_site->domain == "example.com") {
    echo "Welcome to the Example.com network!";
}

Redirect users based on the current network domain

This example redirects users to a specific page based on the current network domain.

$current_site = get_current_site();

switch ($current_site->domain) {
    case "example1.com":
        header("Location: https://example1.com/special-page");
        break;
    case "example2.com":
        header("Location: https://example2.com/another-page");
        break;
}

Use current network ID in a function

This example uses the current network ID to perform a task based on the network ID.

function do_something_based_on_network($network_id) {
    // Your custom code here
}

$current_site = get_current_site();
do_something_based_on_network($current_site->id);