The activate_plugins() WordPress PHP function activates a single plugin or a list of plugins. If it returns a WP_Error, it means that one or more of the plugin file paths were invalid, and not necessarily that a plugin had errors. The execution will be halted immediately if one of the plugins encounters an error.
Usage
Here’s a simple example of how to use the activate_plugins() function:
activate_plugins('my-plugin/my-plugin.php');
In this case, we are activating a plugin named ‘my-plugin’.
Parameters
- $plugins (string|array): This is a required parameter. It is the name or list of the plugins to activate.
- $redirect (string): This is an optional parameter. You can use it to redirect to a page after successful activation. The default value is ” (empty string).
- $network_wide (bool): This is an optional parameter. Set it to true if you want to enable the plugin for all sites in the network. The default value is false.
- $silent (bool): This is an optional parameter. If set to true, it prevents calling activation hooks. The default value is false.
More information
See WordPress Developer Resources: activate_plugins()
Examples
Activating a Single Plugin
This code activates a single plugin named ‘my-plugin’.
activate_plugins('my-plugin/my-plugin.php');
Activating Multiple Plugins
This code activates multiple plugins.
activate_plugins(array('my-plugin/my-plugin.php', 'another-plugin/another-plugin.php'));
Activating a Plugin and Redirecting
This code activates a plugin and then redirects to a custom page.
activate_plugins('my-plugin/my-plugin.php', admin_url('my-page.php'));
Activating a Plugin Network-Wide
This code activates a plugin for all sites in a network.
activate_plugins('my-plugin/my-plugin.php', '', true);
Activating a Plugin Silently
This code activates a plugin without calling activation hooks.
activate_plugins('my-plugin/my-plugin.php', '', false, true);