Deactivate plugin upon deactivation of another plugin

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…

Related posts

Leave a Reply

2 comments

  1. 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);
       }