If possible, how can installed plugins (meaning the files have been placed in wp-content/plugins directory) be activated from other plugins?


This is how I did it in some web apps:

function run_activate_plugin( $plugin ) {
    $current = get_option( 'active_plugins' );
    $plugin = plugin_basename( trim( $plugin ) );

    if ( !in_array( $plugin, $current ) ) {
        $current[] = $plugin;
        sort( $current );
        do_action( 'activate_plugin', trim( $plugin ) );
        update_option( 'active_plugins', $current );
        do_action( 'activate_' . trim( $plugin ) );
        do_action( 'activated_plugin', trim( $plugin) );

    return null;
run_activate_plugin( 'akismet/akismet.php' );
| improve this answer | |
  • 1
    As Rarst wrote, this is not very "safe". You should make sure the plugins you activate this way are safe / will not break your site. – sorich87 Nov 14 '10 at 15:46
  • 10
    Not safe at all, even, but heck, if you're activating them programmatically you're supposed to know what you're doing. ;-) – Denis de Bernardy Nov 14 '10 at 19:45
  • 3
    This, at least more recently, is reinventing the wheel. I have used the built-in activate_plugin(), and it seems to work the same. – Jake Sep 18 '13 at 18:21
  • 2
    @SagiveSEO Its perfectly safe - its similar to how WP does it, except by requiring user input/approval and form nonces and checks for those nonces - just introduce necessary checks for initiating that function anywhere - so that it cannot be accessed from outside (from web etc), and it cannot be randomly called in any way. – unity100 Oct 24 '18 at 1:38
  • 1
    Just dropping a comment to agree with @unity100 and to disarm these "not safe" comments. This is a perfectly fine way to do. It's about where and how you will use it, that can make it unsafe. But that goes for just about any piece of code you copy from the internetz. – maryisdead Apr 24 at 10:44

Plugin activation process is coded to work with WP admin interface. It performs some checks to prevent enabling plugins with errors (loading such on start might break WP).

It is handled by activate_plugin() function (source) which is documented as unusable elsewhere.

So if you want to activate plugin by code the goal itself is relatively easy - to change active_plugins option to include that plugin. But you will have to re-create related activation hooks from scratch and will risk breaking site by activating without sandbox step.

| improve this answer | |

Plugins are stored in an array in the 'active_plugins' option. The array contains the file path to each plugin that is active.

To activate a plugin you need to determine what it's path will be, then pass that path to activate_plugin($plugin_path).

This is easier said than done though, and (at least in 2.9) the core code does not make it easy.

Before you can activate_plugin() you need to include the plugin.php file from wp-admin/includes/. You should also check to make sure your plugin isn't already active. The result looks something like this (YMMV):

// Define the new plugin you want to activate
$plugin_path = '/path/to/your/new/plugin.php';
// Get already-active plugins   
$active_plugins = get_option('active_plugins');
// Make sure your plugin isn't active
if (isset($active_plugins[$plugin_path]))

// Include the plugin.php file so you have access to the activate_plugin() function
require_once(ABSPATH .'/wp-admin/includes/plugin.php');
// Activate your plugin

I use this on production in WP 2.9 and have not had any major issues but in my testing it had very unexpected results with WPMU, so beware using this on network installs.

| improve this answer | |
  • a long time passed but if you remember, what was unexpected result on this? – Ünsal Korkmaz Aug 25 '12 at 10:59
  • I completely don't remember. I think it just didn't work and I did things by hand because I didn't have too many MS sites. – jerclarke Aug 28 '12 at 17:48
  • how do I execute this? if i just execute it directly, get_option is not defined... – Janus Troelsen Aug 25 '16 at 9:45
  • Add this in functions.php and it should work, though even then you should always put things in functions and hook them to "init" or "wp". – jerclarke Aug 26 '16 at 14:57

Wordpress provides a function for activating plugins, according to WP Codex

function plugin_activation( $plugin ) {
    if( ! function_exists('activate_plugin') ) {
        require_once ABSPATH . 'wp-admin/includes/plugin.php';

    if( ! is_plugin_active( $plugin ) ) {
        activate_plugin( $plugin );

| improve this answer | |
  • Ethan, what is wrong with my answer? – Mowshon Oct 28 '16 at 14:59
  • While posting just the code is okay for an answer, it may be hard to interpret for any new programmers that read your answer. This way, OP can have a better understanding on how your code works. – Ethan O'Sullivan Oct 28 '16 at 21:32

You can simply call the WordPress default function.


Please check below link for more detail.

activate plugin

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.