Using WordPress ‘network_domain_check()’ PHP function

The network_domain_check() WordPress PHP function checks for an existing network.

Usage

A generic example of how to use the function:

$network_exists = network_domain_check();
if ($network_exists) {
    echo "Network exists!";
} else {
    echo "No network found.";
}

Parameters

This function has no parameters.

More information

See WordPress Developer Resources: network_domain_check()

Examples

Check if the network exists and display a message

This code checks if a network exists and displays a corresponding message.

$network_exists = network_domain_check();
if ($network_exists) {
    echo "Network exists!";
} else {
    echo "No network found.";
}

Redirect users to a custom page if no network is found

This code redirects users to a custom page if no network is found.

$network_exists = network_domain_check();
if (!$network_exists) {
    wp_redirect('https://example.com/no-network-found');
    exit;
}

Display specific content based on network existence

This code displays specific content based on whether a network exists or not.

$network_exists = network_domain_check();
if ($network_exists) {
    echo "Welcome to our network!";
} else {
    echo "Welcome to our standalone site!";
}

Conditionally load a plugin based on network existence

This code loads a plugin only if a network exists.

$network_exists = network_domain_check();
if ($network_exists) {
    include_once(WP_PLUGIN_DIR . '/network-plugin/network-plugin.php');
}

Perform a network-specific action only if a network exists

This code performs a network-specific action only if a network exists.

$network_exists = network_domain_check();
if ($network_exists) {
    // Perform a network-specific action
    wp_schedule_event(time(), 'daily', 'my_network_daily_cron');
}