Using WordPress ‘activate_plugin()’ PHP function

The activate_plugin() WordPress PHP function is used to activate a plugin within a protected environment, known as a “sandbox”. If the plugin is already active, it will not attempt to reactivation. This function sets a redirection to an error before trying to load the plugin file. If the plugin fails to load, the redirection will not be overwritten with a success message. This function will not prevent errors within the plugin file itself and should not be used to replicate the “sandbox” environment elsewhere.

Usage

Here’s a simple example:

$result = activate_plugin('my-plugin/my-plugin.php');
if (is_wp_error($result)) {
    // Handle the error
}

In this case, ‘my-plugin/my-plugin.php’ is the path to the plugin file relative to the plugins directory.

Parameters

  • $plugin (string) – Required. This is the path to the plugin file relative to the plugins directory.
  • $redirect (string) – Optional. This is the URL to redirect to after the function is executed. The default value is an empty string (”).
  • $network_wide (bool) – Optional. This decides whether to activate the plugin for all sites in the network (true) or just the current site (false). This is only applicable for multisite installations. The default value is false.
  • $silent (bool) – Optional. When set to true, the function will prevent calling activation hooks. The default value is false.

More information

See WordPress Developer Resources: activate_plugin()

This function is included in WordPress and there is no information about it being deprecated as of now.

Examples

Basic Plugin Activation

This example tries to activate a plugin, and if it fails, it returns a WP_Error.

$result = activate_plugin('plugin-dir/plugin-file.php');
if (is_wp_error($result)) {
    // Process the error
}

Plugin Activation with Redirection

If you want to use redirection (upon plugin activation), you should only do that if your plugin is not “activated” through “BULK ACTIVATION”. This example redirects to a specific URL after the plugin is activated.

add_action('activated_plugin', 'my_plugin_redirection');

function my_plugin_redirection($plugin) { $table = new WP_Plugins_List_Table; if (plugin_basename(FILE) === $plugin && 'activated-selected' !== $table->current_action()) { wp_redirect('your-url-here'); exit(); } }

Network Wide Plugin Activation

This example activates a plugin for all sites in a multisite network.

$result = activate_plugin('plugin-dir/plugin-file.php', '', true);
if (is_wp_error($result)) {
    // Process the error
}

Silent Plugin Activation

This example activates a plugin without calling the activation hooks.

$result = activate_plugin('plugin-dir/plugin-file.php', '', false, true);
if (is_wp_error($result)) {
    // Process the error
}

Plugin Activation with Custom Redirection

This example activates a plugin and redirects to a specific URL upon successful activation.

$result = activate_plugin('plugin-dir/plugin-file.php', 'http://yourwebsite.com/your-page');
if (is_wp_error($result)) {
    // Process the error
}