Title: activate_plugin
Published: April 25, 2014
Last modified: February 24, 2026

---

# activate_plugin( string $plugin, string $redirect, bool $network_wide = false, bool $silent = false ): null|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#wp--skip-link--target)

Attempts activation of plugin in a “sandbox” and redirects on success.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#description)󠁿

A plugin that is already activated will not attempt to be activated again.

The way it works is by setting the redirection to the error before trying to include
the plugin file. If the plugin fails, then the redirection will not be overwritten
with the success message. Also, the options will not be updated and the activation
hook will not be called on plugin error.

It should be noted that in no way the below code will actually prevent errors within
the file. The code should not be used elsewhere to replicate the “sandbox”, which
uses redirection to work.
{@source 13 1}

If any errors are found or text is outputted, then it will be captured to ensure
that the success redirection will update the error redirection.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#parameters)󠁿

 `$plugin`stringrequired

Path to the plugin file relative to the plugins directory.

`$redirect`stringoptional

URL to redirect to.

`$network_wide`booloptional

Whether to enable the plugin for all sites in the network or just the current site.
Multisite only.

Default:`false`

`$silent`booloptional

Whether to prevent calling activation hooks.

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#return)󠁿

 null|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) Null
on success, [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
on invalid file.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#more-information)󠁿

Plugin will fail to activate with the following generic response for multiple reasons,
including; issues parsing the header information, issue with the ‘plugin’ cache (
see [WordPress Object Cache](https://developer.wordpress.org/reference/classes/wp_object_cache/)),
or a permissions error.

    ```php
    The plugin does not have a valid header.
    ```

Issues with the plugin cache, are caused when the plugin files are added or modified,
after the plugins have all been initialised. This can be resolved by reloading the
page, sending the `activate_plugin()` as a separate AJAX request, or if necessary,
by manually updating the cache. Example below:

    ```php
    $cache_plugins = wp_cache_get( 'plugins', 'plugins' );
    if ( !empty( $cache_plugins ) ) {
    $new_plugin = array(
    'Name' => $plugin_name,
    'PluginURI' => $plugin_uri,
    'Version' => $plugin_version,
    'Description' => $plugin_description,
    'Author' => $author_name,
    'AuthorURI' => $author_uri,
    'TextDomain' => '',
    'DomainPath' => '',
    'Network' => '',
    'Title' => $plugin_name,
    'AuthorName' => $author_name,
    );
    $cache_plugins[''][$plugin_path] = $new_plugin;
    wp_cache_set( 'plugins', $cache_plugins, 'plugins' );
    }
    ```

## 󠀁[Source](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#source)󠁿

    ```php
    function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
    	$plugin = plugin_basename( trim( $plugin ) );

    	if ( is_multisite() && ( $network_wide || is_network_only_plugin( $plugin ) ) ) {
    		$network_wide        = true;
    		$current             = get_site_option( 'active_sitewide_plugins', array() );
    		$_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
    	} else {
    		$current = get_option( 'active_plugins', array() );
    	}

    	$valid = validate_plugin( $plugin );
    	if ( is_wp_error( $valid ) ) {
    		return $valid;
    	}

    	$requirements = validate_plugin_requirements( $plugin );
    	if ( is_wp_error( $requirements ) ) {
    		return $requirements;
    	}

    	if ( $network_wide && ! isset( $current[ $plugin ] )
    		|| ! $network_wide && ! in_array( $plugin, $current, true )
    	) {
    		if ( ! empty( $redirect ) ) {
    			// We'll override this later if the plugin can be included without fatal error.
    			wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) );
    		}

    		ob_start();

    		// Load the plugin to test whether it throws any errors.
    		plugin_sandbox_scrape( $plugin );

    		if ( ! $silent ) {
    			/**
    			 * Fires before a plugin is activated.
    			 *
    			 * If a plugin is silently activated (such as during an update),
    			 * this hook does not fire.
    			 *
    			 * @since 2.9.0
    			 *
    			 * @param string $plugin       Path to the plugin file relative to the plugins directory.
    			 * @param bool   $network_wide Whether to enable the plugin for all sites in the network
    			 *                             or just the current site. Multisite only. Default false.
    			 */
    			do_action( 'activate_plugin', $plugin, $network_wide );

    			/**
    			 * Fires as a specific plugin is being activated.
    			 *
    			 * This hook is the "activation" hook used internally by register_activation_hook().
    			 * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
    			 *
    			 * If a plugin is silently activated (such as during an update), this hook does not fire.
    			 *
    			 * @since 2.0.0
    			 *
    			 * @param bool $network_wide Whether to enable the plugin for all sites in the network
    			 *                           or just the current site. Multisite only. Default false.
    			 */
    			do_action( "activate_{$plugin}", $network_wide );
    		}

    		if ( $network_wide ) {
    			$current            = get_site_option( 'active_sitewide_plugins', array() );
    			$current[ $plugin ] = time();
    			update_site_option( 'active_sitewide_plugins', $current );
    		} else {
    			$current   = get_option( 'active_plugins', array() );
    			$current[] = $plugin;
    			sort( $current );
    			update_option( 'active_plugins', $current );
    		}

    		if ( ! $silent ) {
    			/**
    			 * Fires after a plugin has been activated.
    			 *
    			 * If a plugin is silently activated (such as during an update),
    			 * this hook does not fire.
    			 *
    			 * @since 2.9.0
    			 *
    			 * @param string $plugin       Path to the plugin file relative to the plugins directory.
    			 * @param bool   $network_wide Whether to enable the plugin for all sites in the network
    			 *                             or just the current site. Multisite only. Default false.
    			 */
    			do_action( 'activated_plugin', $plugin, $network_wide );
    		}

    		if ( ob_get_length() > 0 ) {
    			$output = ob_get_clean();
    			return new WP_Error( 'unexpected_output', __( 'The plugin generated unexpected output.' ), $output );
    		}

    		ob_end_clean();
    	}

    	return null;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/plugin.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/plugin.php#L641)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/plugin.php#L641-L742)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#hooks)󠁿

 [do_action( ‘activated_plugin’, string $plugin, bool $network_wide )](https://developer.wordpress.org/reference/hooks/activated_plugin/)

Fires after a plugin has been activated.

 [do_action( “activate_{$plugin}”, bool $network_wide )](https://developer.wordpress.org/reference/hooks/activate_plugin/)

Fires as a specific plugin is being activated.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#related)󠁿

| Uses | Description | 
| [validate_plugin_requirements()](https://developer.wordpress.org/reference/functions/validate_plugin_requirements/)`wp-admin/includes/plugin.php` |

Validates the plugin requirements for WordPress version and PHP version.

  | 
| [validate_plugin()](https://developer.wordpress.org/reference/functions/validate_plugin/)`wp-admin/includes/plugin.php` |

Validates the plugin path.

  | 
| [is_network_only_plugin()](https://developer.wordpress.org/reference/functions/is_network_only_plugin/)`wp-admin/includes/plugin.php` |

Checks for “Network: true” in the plugin header to see if this should be activated only as a network wide plugin. The plugin would also work when Multisite is not enabled.

  | 
| [plugin_sandbox_scrape()](https://developer.wordpress.org/reference/functions/plugin_sandbox_scrape/)`wp-admin/includes/plugin.php` |

Loads a given plugin attempt to generate errors.

  | 
| [wp_redirect()](https://developer.wordpress.org/reference/functions/wp_redirect/)`wp-includes/pluggable.php` |

Redirects to another page.

  | 
| [plugin_basename()](https://developer.wordpress.org/reference/functions/plugin_basename/)`wp-includes/plugin.php` |

Gets the basename of a plugin.

  | 
| [update_site_option()](https://developer.wordpress.org/reference/functions/update_site_option/)`wp-includes/option.php` |

Updates the value of an option that was already added for the current network.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [wp_create_nonce()](https://developer.wordpress.org/reference/functions/wp_create_nonce/)`wp-includes/pluggable.php` |

Creates a cryptographic token tied to a specific action, user, user session, and window of time.

  | 
| [is_multisite()](https://developer.wordpress.org/reference/functions/is_multisite/)`wp-includes/load.php` |

Determines whether Multisite is enabled.

  | 
| [add_query_arg()](https://developer.wordpress.org/reference/functions/add_query_arg/)`wp-includes/functions.php` |

Retrieves a modified URL query string.

  | 
| [do_action()](https://developer.wordpress.org/reference/functions/do_action/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to an action hook.

  | 
| [get_site_option()](https://developer.wordpress.org/reference/functions/get_site_option/)`wp-includes/option.php` |

Retrieve an option value for the current network based on name of option.

  | 
| [update_option()](https://developer.wordpress.org/reference/functions/update_option/)`wp-includes/option.php` |

Updates the value of an option that was already added.

  | 
| [get_option()](https://developer.wordpress.org/reference/functions/get_option/)`wp-includes/option.php` |

Retrieves an option value based on an option name.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 12 more](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#)

| Used by | Description | 
| [wp_ajax_activate_plugin()](https://developer.wordpress.org/reference/functions/wp_ajax_activate_plugin/)`wp-admin/includes/ajax-actions.php` |

Handles activating a plugin via AJAX.

  | 
| [WP_REST_Plugins_Controller::handle_plugin_status()](https://developer.wordpress.org/reference/classes/wp_rest_plugins_controller/handle_plugin_status/)`wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php` |

Handle updating a plugin’s status.

  | 
| [activate_plugins()](https://developer.wordpress.org/reference/functions/activate_plugins/)`wp-admin/includes/plugin.php` |

Activates multiple plugins.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.2.0](https://developer.wordpress.org/reference/since/5.2.0/) | Test for WordPress version and PHP version compatibility. | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#comment-content-1202)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/activate_plugin/#comment-1202)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Factivate_plugin%2F%23comment-1202)
     Vote results for this note: 5[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Factivate_plugin%2F%23comment-1202)
 4.  **Basic Example**
 5.  Attempts to activate the plugin, and returns [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
     on failure
 6.      ```php
         $result = activate_plugin( 'plugin-dir/plugin-file.php' );
         if ( is_wp_error( $result ) ) {
         	// Process Error
         }
         ```
     
 7.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Factivate_plugin%2F%3Freplytocom%3D1202%23feedback-editor-1202)
 8.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/activate_plugin/?output_format=md#comment-content-4556)
 9.    [ttodua](https://profiles.wordpress.org/ttodua/)  [  5 years ago  ](https://developer.wordpress.org/reference/functions/activate_plugin/#comment-4556)
 10. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Factivate_plugin%2F%23comment-4556)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Factivate_plugin%2F%23comment-4556)
 11. People, if you want to use redirection (upon plugin activation) **you should only
     do** that if your plugin is not “activated” through “BULK ACTIVATION”!
 12.     ```php
         add_action( 'activated_plugin', 'wpdocs_my_redirection' );
         function wpdocs_my_redirection( $plugin ) {
             $table = new WP_Plugins_List_Table;
             if ( plugin_basename( __FILE__ ) === $plugin && 'activated-selected' !== $table->current_action() ) {  
                wp_redirect( ... );
                exit(); 
             } 
         }
         ```
     
 13.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Factivate_plugin%2F%3Freplytocom%3D4556%23feedback-editor-4556)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Factivate_plugin%2F)
before being able to contribute a note or feedback.