How to deactivate an add-on on deactivation of the main plugin

wp plugins
wp plugins

WordPress plugin directory has nearly 35k plugins and thousands of other premium plugins in the marketplace out of the WordPress plugin directory. Many plugins itself have hundreds of add ons to the main plugin. WooCommerce itself have some close to 300 add-ons or extensions in the official woothemes site and many other you could find in a market place like Envato.

While building an add-on, it is important to build the functionality to check if the main plugin is active during the activation of the add-on and similarly it is best to deactivate the add-on on deactivation of the main plugin. This should be followed and are the best practices to avoid any conflicts, errors or warnings the add-on would throw other wise.

While activating the add-on plugin, the active status of the plugin can be checked by the function is_plugin_active. This can be used on the register activation hook which fires on the time of the plugin activation. The code below checks it on add-on activation

The above checks the whether the main plugin, here for example contact form 7 is active or not, if yes then the add-on plugin gets activated or else the execution dies with an error message.

Now once we have this in place, the condition comes up when the main plugin is deactivated, and we as an add-on developer want the add-on plugin to get deactivated as well with the main plugin. This can be done by hooking our function to the deactivate_PLUGINNAME hook, where PLUGINNAME stands for the plugin directory slash(/) the main plugin file. For example the plugin I am using as an example is contact form 7 so the hook here would be deactivate_contact-form-7/wp-contact-form-7.php

We can now use this hook along with the function, deactivate_plugins to deactivate the add-on plugin along with the main plugin deactivation. But having said that using deactivate plugins function directly in the action hook wont work. This is beacause when the deactivate_PLUGINNAME hook is called the WordPress goes into a series of steps before updating the database with the list of active and inactive plugins. To know more about the steps, give this blog by Stephen Harris a read, so now in short we would call the deactivate plugins function with another hook update_option_active_plugins to make it work.

The code would look something like below

The above will deactivate the add on plugin on deactivation of the main plugin.


Maruti Mohanty

I am a happy go lucky WordPress Developer, who loves biking, movies and learning different languages


You may also like...

2 Responses

  1. Interesting. And where does one get the deactivation action from? The main plugin?

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: