2

Wondering if its possible to deactivate a plugin in the deactivation code of another plugin?

IE. i have a widget that i'm adding via its own plugin that won't function if the 'master' plugin isn't activated...

1

Yes, register a deactivation hook in your first plugin, and inside this hook deactivate the 2nd plugin using the deactivate_plugins() function

9

Note: After writing this, I submitted a trac ticket, only to be told that this one already existed.

@OneTrickPony's answer didn't work forme, and after inspecting the source (specifically deactivate_plugins()) I found why:

Let's suppose B depends on A, and A is deactivated by the user. WordPress calls deactivate_plugins(A).

This function does the following:

  1. Gets all the current active plugins.]
  2. Performs some checks (e.g. is plugin A actually active?)
  3. Removes A from this array
  4. Fires the hook deactivate_A (which we hook onto using register_deactivation_hook)
  5. Updates the array to the database.

Now at step 4, we call deactivate_plugins(B) to deactivate. The same process happens again, and is completed- that's fine. But once that's completed we proceed to step 5 (in the original deactivate_plugins() call for A). The array is updated to the database - but this array was the very original one retrieved in step 1 and only has A removed. In particular we retrieved it at the beginning when B was still active, and so it contains B.

Note: your deactivation callbacks are fired, even through WordPress still thinks its active next time the page loads.

The solution

The solution is to use a later hook (after the option has been updated). For this we can take advantage of the update_option_{$option} hook:

//This goes inside Plugin A.
//When A is deactivated. Deactivate B.
  register_deactivation_hook(__FILE__,'my_plugin_A_deactivate'); 
  function my_plugin_A_deactivate(){
     $dependent = 'B/B.php';
     if( is_plugin_active($dependent) ){
          add_action('update_option_active_plugins', 'my_deactivate_dependent_B');
     }
   }

   function my_deactivate_dependent_B(){
       $dependent = 'B/B.php';
       deactivate_plugins($dependent);
   }
  • How i can add a admin notice that dependent plugin is deactivated due to the required plugin deactivated ? – Varun Sridharan Feb 28 '16 at 3:42
  • You could wait until admin_notices to deactivate the dependant plug-in and so print a message before hand. Personally though, I would just leave the dependent plug-in active, but simply do not load any functionality, except for an admin message inviting the user to reactivate the required plug-in. – Stephen Harris Feb 29 '16 at 15:28
  • This is all great when you write 2 plugins in question. But what happens when you depend on a third party plugin? I can control not activating my plugin if the other isn't active, but I cannot deactivate my plugin when the other one is deactivated. – dingo_d Dec 8 '18 at 17:14
  • @dingo_d I suppose you could hook into update_option_active_plugins in plug-in B (your plug-in) and check the array for your dependencies (e.g. plugin A). If it's not there, and your plugin is, deactivate it. But I would advise against this. If I activate plugin B without A, it'd just immediately be deactivated. A better UX would be to have your plug-in to do nothing expect display a message prompting the user to install/activate A. You can even provide a activate link if its installed. – Stephen Harris Dec 8 '18 at 22:27
  • Yeah, in my case I depend on WooCommerce, and if you accidentally deactivate WooCommerce before deactivating my plugin, you'll get a white screen of death. That's what I want to prevent. I need to see what hooks trigger when WooCommerce is deactivated and see if I can find a hook to preform my plugin deactivation (even if it is by manually querying the wp_options table and removing it from the list) – dingo_d Dec 9 '18 at 10:06

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.