The activate_sitewide_plugin() WordPress PHP function is a deprecated function used for activating a network-only plugin. It’s recommended to use activate_plugin() instead.
Usage
Let’s say you have a network-only plugin ‘my_plugin/my_plugin.php’ and you want to activate it:
activate_sitewide_plugin( 'my_plugin/my_plugin.php' );
Note: This function doesn’t return any value.
Parameters
- No parameters are accepted by this function.
More information
See WordPress Developer Resources: activate_sitewide_plugin()
This function is deprecated in favor of activate_plugin(). There’s no specific version number when it was implemented or deprecated. The source code can be found in the WordPress core files.
Examples
Activating a Network-Only Plugin
If you have a network-only plugin you wish to activate, you can use activate_sitewide_plugin():
// Define the plugin file $plugin_file = 'my_plugin/my_plugin.php'; // Activate the plugin activate_sitewide_plugin( $plugin_file );
This will activate the ‘my_plugin’ across the entire network.
Activating Multiple Network-Only Plugins
You can also use this function in a loop to activate multiple network-only plugins:
// Define the plugin files $plugin_files = array( 'my_plugin1/my_plugin1.php', 'my_plugin2/my_plugin2.php' ); // Loop through each plugin and activate it foreach ( $plugin_files as $plugin_file ) { activate_sitewide_plugin( $plugin_file ); }
This will activate ‘my_plugin1’ and ‘my_plugin2’ across the entire network.
Activating a Plugin Only If It’s Not Already Active
Before using activate_sitewide_plugin(), you may want to check if the plugin is already active:
// Define the plugin file $plugin_file = 'my_plugin/my_plugin.php'; // Check if the plugin is active if ( ! is_plugin_active( $plugin_file ) ) { // If the plugin is not active, activate it activate_sitewide_plugin( $plugin_file ); }
This will activate ‘my_plugin’ only if it’s not already active.
Activating a Plugin and Handling Errors
When activating a plugin, it’s a good idea to handle potential errors:
// Define the plugin file $plugin_file = 'my_plugin/my_plugin.php'; // Try to activate the plugin $result = activate_sitewide_plugin( $plugin_file ); // Check if there was an error if ( is_wp_error( $result ) ) { // Handle the error echo 'Error activating plugin: ' . $result->get_error_message(); }
This will attempt to activate ‘my_plugin’, and output an error message if there’s a problem.
Using activate_plugin() Instead of activate_sitewide_plugin()
As activate_sitewide_plugin() is deprecated, it’s better to use activate_plugin() instead:
// Define the plugin file $plugin_file = 'my_plugin/my_plugin.php'; // Activate the plugin activate_plugin( $plugin_file, '', true );
This will activate ‘my_plugin’ across the entire network, using the activate_plugin() function. The third parameter being ‘true’ indicates that the plugin should be activated network-wide.