activate_plugin() wordpress function and usage

Home / WordPress / WordPress Wiki / activate_plugin() wordpress function and usage

activate_plugin( string $plugin, string $redirect = '', bool $network_wide = false, bool $silent = false )
This function will attempts activation of plugin in a “sandbox” and redirects on success.


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.

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.



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


(string) (Optional) URL to redirect to.

Default value: ”


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

Default value: false


(bool) (Optional) Whether to prevent calling activation hooks.

Default value: false


(null|WP_Error) Null on success, WP_Error on invalid file.

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, or a permissions error.

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:

$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' );


Usage sample

Attempts to activate the plugin, and returns WP_Error on failure

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

Iif you want to use redirection (upon plugin activation) you should only do that if your plugin is not “activated” through “BULK ACTIVATION”!

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( ... );

If you have question on this function further usage, kindly make use of the comment box

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments