How to Programmatically Activate a Plugin in WordPress

5 from 5
from 2 user
(Rate this post)

Learn how to activate a plugin programmatically in WP by using the activate_plugin function. In this WordPress tutorial, we'll review the activate_plugin() function and teach you how to activate a plugin in WordPress using PHP, not the admin panel.

Scroll to the bottom of the tutorial to see examples (or click here).

activate_plugin Function Introduction

This function is useful when you want to activate a plug-in using PHP codes when developing WordPress.

The activate_plugin function in WordPress accepts one required parameter and three optional parameters.

The required parameter is the path to the plugin’s main file.  the other three are just for customizing your activation request.

If there were no errors in the plugin activation, nothing would be returned. If you specify a redirection URL, this function will redirect the browser to the URL on success.

In case of failure, the activate_plugin will return WP_Error.

This is the basic usage if you want to activate ‘my-custom-plugin’ from the plugins directory:

activate_plugin( 'my-custom-plugin/my-custom-plugin.php' );

Keep reading for the complete usage guide if you need more information about the optional parameters and use cases.

If you do not know how to create a WP plugin, read my tutorial on creating a WordPress plugin.

activate_plugin Function Syntax

As I mentioned above, activate_plugins accepts one required parameter, which is the path to the main PHP file of your plugin. The other three parameters are optional:

activate_plugin( string $plugin,  string $redirect = '',  bool $network_wide = false,  bool $silent = false )

activate_plugin Function Parameters

  • $plugin: The relative or full path to the plugin’s main file.
  • $redirect: The URL you want to redirect to on success. Do not specify or set '' if no redirection is needed.
  • $network_wide: Activate the plugin for the current website or all websites in WP multisite installation. Do not specify in a normal WP or set false.
  • $silent: If set to true, activation hooks will not fire. The default value is false.

activate_plugin Function Result

In case of failure, the WP_Error object will return. This way, you can detect failure using the is_wp_error() function of WordPress.

Do NOT use the result of the activation_plugin function solely in a condition because there is no return if the process is successful.

Use is_wp_error( activate_plugin( $plugin ) ) to check if there was any problem in the activation process.

activate_plugin Function Examples

I have two examples for you:

A basic example of using activate_plugin to activate a plugin in WordPress:

//if you try activate_plugin() outside of function.php or activation hooks, you need to include these fule before.
	if ( ! function_exists( 'activate_plugin' ) ) {
		include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
	}

	//you may get error that wp_redirect does not exist (which activate_plugin relies on)
	if ( ! function_exists( 'wp_redirect' ) ) {
		include_once( ABSPATH . 'wp-includes/pluggable.php' );
	}

	//use the relative or full path to the plugin's main php file (not only plugin name!)
	$pluginToActivate = 'my-custom-plugin/my-custom-plugin.php';

	//set the url for redirection (no need to set it if you do not want to redirect)
	//calling activate_plugin() from register_activation_hook of another plugin will not redirect.
	$redirectUrlOnSuccess = site_url( '/my-success-url' );

	//useful for WP multisite. set true if you are using WP multisite (i guess you're not!).
	//if you set true for normal WP, activate_plugin() will not work.
	$networkWide = false;

	//prevents execution of activate_plugin hook for this plugin.
	$silentActivation = false;

	//try to activate the plugin
	$tryToActivate = activate_plugin( $pluginToActivate, $redirectUrlOnSuccess, $networkWide, $silentActivation );

	//catch errors if activation failed
	if ( is_wp_error( $tryToActivate ) ) {
		var_dump( $tryToActivate );
	}

Activating another plugin programmatically when activating a plugin in the admin panel:

Scenario: you have two plugins, my-first-plugin and my-second-plugin. You want to activate my-second-plugin when the my-first-plugin is getting activated through the admin panel.

Pu the below codes inside the plugins/my-first-plugin/my-first-plugin.php file (first plugin’s main file).

//my-first-plugin activation callback.
//plugins/my-first-plugin/my-first-plugin.php
function myFirstPluginActivationCallback(){
	
	//activate my-second-plugin 
	$tryToActivate = activate_plugin( 'my-second-plugin/my-second-plugin.php' );

	//catch errors if activation failed
	if ( is_wp_error( $tryToActivate ) ) {
		var_dump( $tryToActivate );
	}
	
}

register_activation_hook( __FILE__, 'myFirstPluginActivationCallback' );

Now, if you activate my-first-plugin inside the WordPress admin panel, the my-second-plugin is getting activated automatically.

What do you think about "How to Programmatically Activate a Plugin in WordPress"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu