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.

I am trying to activate second plugin automatically while activating first plugin.

register_activation_hook(__FILE__, 'example_activation' );
function example_activation() {
        include_once(ABSPATH .'/wp-admin/includes/plugin.php');
        activate_plugin('hello.php');
}

Its not working inside register_activation_hook.. Its working if i use it directly like:

include_once(ABSPATH .'/wp-admin/includes/plugin.php');
activate_plugin('hello.php');

How can i fix it? Thanks for help

Solution:

I am using this for myself now:

// When this plugin activate, activate another plugin too.
register_activation_hook(__FILE__, function(){
    $dependent = 'hello.php';
    if( is_plugin_inactive($dependent) ){
        add_action('update_option_active_plugins', function($dependent){
            /* for some reason, 
            activate_plugin($dependent); 
               is not working */
            activate_plugin('hello.php');
        });
    }
}); 

// When this plugin deactivate, deactivate another plugin too.
register_deactivation_hook(__FILE__, function(){
    $dependent = 'hello.php';
    if( is_plugin_active($dependent) ){
        add_action('update_option_active_plugins', function($dependent){
            deactivate_plugins('hello.php');
        });
    }
}); 
share|improve this question
    
I misread your post originally. I got to say, it is formatted correctly. Is this function in the main of your plugin? –  Chris Aug 24 '12 at 16:52
    
Just a second. Need to edit my post. –  Chris Aug 24 '12 at 20:25
    
yes its in first plugin which trying to activate second plugin.. second plugin is hello.php as example –  Ünsal Korkmaz Aug 24 '12 at 22:43

2 Answers 2

up vote 7 down vote accepted
+50

For full explanation of what's happening see this post (this is for deactivating plug-ins, but the problem is the same).

A brief explanation: Plug-ins are essentially activated by adding them to the array of active pug-ins stored in the database. When you activate the first plug-in, WordPress retrieves the array of all currently active plug-ins, adds the plug-in to it (but doesn't update the database yet) and then runs your installation callback.

This installation callback runs your code.

After that WordPress updates the database with the above array, which contains the first but not the second plug-in. Thus your second plug-in appears not be activated.

Solution: has mentioned in the above link the solution is something like this (untested):

//This goes inside Plugin A.
//When A is activated. activate B.
register_activation_hook(__FILE__,'my_plugin_A_activate'); 
function my_plugin_A_activate(){
    $dependent = 'B/B.php';
    if( is_plugin_inactive($dependent) ){
         add_action('update_option_active_plugins', 'my_activate_dependent_B');
    }
}

function my_activate_dependent_B(){
    $dependent = 'B/B.php';
    activate_plugin($dependent);
}
share|improve this answer
    
thanks for help :) –  Ünsal Korkmaz Sep 2 '12 at 15:02
    
If u have time, i edit my question to write what i am using now. You can see activate_plugin($dependent); is not working when activating plugin. Any idea how to fix that? –  Ünsal Korkmaz Sep 2 '12 at 15:11
    
Copying your code into a test plug-in (and creating another test hello.php test plug-in - it works fine for me). Remember that if the plug-ins exist in a subdirectory, you'll need to give the folder: e.g. hello/hello.php. –  Stephen Harris Sep 2 '12 at 18:01
    
my code will work normal.. problem is starting when you change activate_plugin('hello.php'); line with activate_plugin($dependent); –  Ünsal Korkmaz Sep 2 '12 at 19:23
    
That's because the variable given to your callback is not hello.php but the array of currently active plug-ins. You can't pass your own variables to callbacks like that. Unless you use class/global variables you'll need to use activate_plugin('hello.php');. But that shouldn't be a problem...? –  Stephen Harris Sep 3 '12 at 10:45

Here is someone explaining why it wasn't working for him, and how he had to copy the activate_plugin method and create a custom one of his own without the problem: http://stackoverflow.com/questions/1415958/how-to-activate-wordpress-plugins-internally

Here is the original source of the activate_plugin code, that you could use to create your own function and see where it's not working for you: http://hitchhackerguide.com/2011/02/11/activate_plugin/

Here is a possible solution, containing an alternative plugin activation method that someone else probably whittled from that source code: How To Activate Plugins via Code?

share|improve this answer
    
that solution still not working in register_activation_hook –  Ünsal Korkmaz Aug 24 '12 at 18:38
    
Well fortunately, now that you have your own method for activating a plugin, you should be able to debug it and figure out where it's failing right? :) Did you change your register_activation_hook to use your custom activate_plugin method instead of the default one? If so, throw in a few trigger_error statements and find out where it's failing. For instance, a good place to start would be to find out what your method is returning when it fails. –  mltsy Aug 28 '12 at 14:24
    
I tried my best for debugging but i couldnt find reason. Maybe active_plugins option getting update after register_activation_hook executed. –  Ünsal Korkmaz Aug 28 '12 at 16:44
    
I'm sorry, I just fixed the order of the two links I had swapped (the last two). Are you using the code from the link titled "How To Activate Plugin via Code?" Another helpful hint to provide would be the return value of the default activate_plugin method. You can use trigger_error to find the return value, and post it here. –  mltsy Aug 28 '12 at 16:53

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.