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…
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…
You must be logged in to post a comment.
Yes, register a deactivation hook in your first plugin, and inside this hook deactivate the 2nd plugin using the
deactivate_plugins()
functionNote: 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:
deactivate_A
(which we hook onto usingregister_deactivation_hook
)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 originaldeactivate_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: