Search

Deactivate a WordPress Plugin Depending on Another Plugin’s Activation State

    Aparna Gawade
Listen to this article

While developing a plugin, especially an add-on or a premium plugin, you would have to handle some use cases. One such use case for example would be, when activating a premium plugin, you would have to deactivate your freemium plugin. Or if your plugin was dependent on other plugins, you could check if those plugins are active before activating your plugin. Custom conditional logic can be added to your plugin, on activation and deactivation hooks.

[space]

Checking if a Dependent Plugin is Active

Say your plugin’s activation is dependent on another plugin. Then, you would need to check if the other plugin is active, before activating your plugin. This conditional logic for activating your plugin, will have to be added using the register_activation_hook provided by WordPress.

register_activation_hook(__FILE__, 'mypluginname_activation_logic');

where ‘mypluginname_activation_logic’ should be written in your main plugin file. In this function, you have to check if the dependent plugin is active.

function mypluginname_activation_logic() {
    //if dependent plugin is not active
    if (!is_plugin_active('abc/abc.php') )
    {
        deactivate_plugins(plugin_basename(__FILE__));
    }
}

WordPress provides you with a functions is_plugin_active to check if the plugin is active. This function basically returns a true or false result. But the drawback here, is that you would have to know the exact plugin folder name and file name.

[space]

A more preferred option, is to check if a class or function created by the plugin exists using, class_exists or function_exists function. For example, the WooCommerce plugin creates the class ‘WooCommerce’. To check if WooCommerce plugin is active you could use:

if (class_exists('Woocommerce')) {
    // your code here
}

But it is obvious that the drawback here is that, if the class or function name changes during plugin updates, this check will not work.
[space]

Deactivating another Plugin from your Plugin

To deactivate another plugin, you can use the function, deactivate_plugins function provided by WordPress. The deactivate function has to be called on the appropriate hook, depending on when the plugin should be deactivated. For example, to deactivate plugin ABC, when plugin XYZ is activated, we will have to hook into the plugin XYZ activation function, and deactivate ABC.

register_activation_hook(__FILE__, 'XYZ_activation');
function XYZ_activation() {
    deactivate_plugins('ABC/ABC.php');
}

The path for the plugin to be deactivated, has to be specified in the deactivate_plugins function.

[space]

Deactivating your Plugin if Dependent Plugin is Deactivated

Such a use case would arise when building a custom add-on or extension on a base plugin. In such a scenario, you would want to deactivate your add-on when the base plugin is deactivated. To do this, you have to hook to the deactivated_plugin action, and check if the plugin deactivated is the base plugin.

function detect_plugin_deactivation( $plugin, $network_activation ) {
    if ($plugin=="abc/abc.php")
    {
        deactivate_plugins(plugin_basename(__FILE__));
    }
}
add_action( 'deactivated_plugin', 'detect_plugin_deactivation', 10, 2 );

[space]

All the above use cases specified are mostly to deactivate a plugin. The reason for this being, it is better not to activate a plugin on behalf of the user. There may be some must use plugins you would want to activate automatically, such plugins can be created and placed inside the must use directory. For more information, you can refer to Must Use WordPress plugins.

 

Aparna Gawade

Aparna Gawade

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads