Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 12 down vote accepted

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' );
share|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
4  
Not safe at all, even, but heck, if you're activating them programmatically you're supposed to know what you're doing. ;-) –  Denis Nov 14 '10 at 19:45
    
Safety aside - I mean, this is not part of the question, right? - I like sorich's approach because it calls the standard activation filters. So the plugins get a change to run their setup routines if they have some. –  hakre Nov 14 '10 at 22:38
    
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
    
i would love to know what not safe about this so i can fix it but other than that this is the first solution to work! thanks... –  Sagive SEO Jun 2 at 22:24
add comment

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.

share|improve this answer
add comment

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]))
    return;

// 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
activate_plugin($plugin_path);

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.

share|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. –  jeremyclarke Aug 28 '12 at 17:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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