WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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...

share|improve this question
up vote 0 down vote accepted

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

share|improve this answer

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.
  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';
share|improve this answer
How i can add a admin notice that dependent plugin is deactivated due to the required plugin deactivated ? – Varun Sridharan Feb 28 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 at 15:28

Your Answer


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.