Using WordPress ‘can_edit_network()’ PHP function

The can_edit_network() WordPress PHP function determines whether a particular network from a page can be edited or not. By default, editing of a network is restricted to the Network Admin for a given $network_id. This function provides a way to override that.

Usage

Here’s a simple example of how this function can be used:

$network_id = 1; // let's assume 1 is the network ID
if (can_edit_network($network_id)) {
    echo "You can edit this network!";
} else {
    echo "Sorry, you cannot edit this network.";
}

In this code, the function can_edit_network() checks if the network with ID 1 can be edited. If it can, “You can edit this network!” will be printed. If not, “Sorry, you cannot edit this network.” will be printed.

Parameters

  • $network_id (int): The network ID to check. This is required.

More Information

See WordPress Developer Resources: can_edit_network()

Please note that the use of this function requires an understanding of WordPress’s multisite feature, as it deals with network-level permissions.

Examples

Check network edit permissions

In this example, we are checking if a network with a given ID can be edited.

$network_id = 2;
if (can_edit_network($network_id)) {
    // If the user can edit the network, display a message
    echo "Network is editable.";
} else {
    // If not, display a different message
    echo "Network is not editable.";
}

Implementing an edit restriction

This example checks if a network can be edited before making changes to it.

$network_id = 3;
if (can_edit_network($network_id)) {
    // Make some changes to the network
} else {
    echo "You do not have permission to edit this network.";
}

Providing alternate functionality

This example provides alternate functionality for users who cannot edit a network.

$network_id = 4;
if (can_edit_network($network_id)) {
    // Edit the network
} else {
    // Provide alternate functionality
}

Checking multiple networks

This example checks if multiple networks can be edited.

$network_ids = array(1, 2, 3, 4, 5);
foreach ($network_ids as $network_id) {
    if (can_edit_network($network_id)) {
        echo "Network $network_id can be edited.";
    } else {
        echo "Network $network_id cannot be edited.";
    }
}

Incorporating into a function

This example incorporates the can_edit_network() function into a custom function.

function check_network_editable($network_id) {
    if (can_edit_network($network_id)) {
        return true;
    } else {
        return false;
    }
}

// Use the function
$network_id = 5;
if (check_network_editable($network_id)) {
    echo "The network is editable.";
} else {
    echo "The network is not editable.";
}