Using WordPress ‘is_network_only_plugin()’ PHP function

The is_network_only_plugin() WordPress PHP function checks for “Network: true” in the plugin header to determine if the plugin should be activated only as a network-wide plugin. This function also supports backward compatibility by checking for “Site Wide Only: true”. The plugin will work even when Multisite is not enabled.

Usage

$is_network_only = is_network_only_plugin( $plugin );

Example:
Input:

$my_plugin = 'my-plugin/my-plugin.php';
$is_network_only = is_network_only_plugin( $my_plugin );

Output:
true or false

Parameters

  • $plugin (string) – Required. Path to the plugin file relative to the plugins directory.

More information

See WordPress Developer Resources: is_network_only_plugin()

Examples

Check if a plugin is network-only

This example checks if a specific plugin is network-only.

$plugin_path = 'my-plugin/my-plugin.php';
$is_network_only = is_network_only_plugin( $plugin_path );
echo $is_network_only ? 'Network-only plugin' : 'Not a network-only plugin';

Display network-only plugins

This example lists all the network-only plugins.

$plugins = get_plugins();
foreach ($plugins as $plugin_path => $plugin_data) {
  if (is_network_only_plugin($plugin_path)) {
    echo $plugin_data['Name'] . '<br>';
  }
}

Disable activation of network-only plugins on a single site

This example prevents network-only plugins from being activated on a single site.

function disable_network_only_plugin_activation( $plugin, $network_wide ) {
  if ( ! $network_wide && is_network_only_plugin( $plugin ) ) {
    wp_die( 'This plugin can only be activated network-wide.' );
  }
}
add_action( 'activate_plugin', 'disable_network_only_plugin_activation', 10, 2 );

Display a custom message for network-only plugins

This example displays a custom message for network-only plugins in the plugins list.

function custom_network_only_plugin_message( $plugin_file, $plugin_data ) {
  if ( is_network_only_plugin( $plugin_file ) ) {
    echo '<span style="color:red;">Network-only plugin</span>';
  }
}
add_action( 'after_plugin_row', 'custom_network_only_plugin_message', 10, 2 );

Deactivate non-network plugins when a network plugin is activated

This example deactivates all non-network plugins when a network-only plugin is activated.

function deactivate_non_network_plugins_on_network_plugin_activation( $plugin, $network_wide ) {
  if ( $network_wide && is_network_only_plugin( $plugin ) ) {
    $plugins = get_plugins();
    foreach ( $plugins as $plugin_path => $plugin_data ) {
      if ( ! is_network_only_plugin( $plugin_path ) && is_plugin_active( $plugin_path ) ) {
        deactivate_plugins( $plugin_path );
      }
    }
  }
}
add_action( 'activate_plugin', 'deactivate_non_network_plugins_on_network_plugin_activation', 10, 2 );