How to deactivate a WordPress plugin automatically

The ability to deactivate a WordPress plugin automatically is a handy feature. It gives a plugin developer the ability to deactivate their plugin when certain criteria are met.

For instance, if you knew a WordPress function you were calling only existed in a certain release of WordPress you could deactivate your plugin if the current WordPress version did not meet the minimum version requirement. Another example would be if you were depending on a custom post type created by another plugin you would want to deactivate your plugin to prevent the user from experiencing unexpected results.

If at any time you have questions about a particular function I have added a References section at the end of this post with some handy links to documentation on the functions.

Overview

There are three basic steps I like to follow when deactivating a plugin automatically…

  1. Check criteria for deactivation
  2. Deactivate the plugin
  3. Display a message to the user why it was deactivated

Those seem simple enough. Read on to see two examples of deactivating a plugin automatically.

Example 1: Wrong version of WordPress

In this example we will check the current version of WordPress against a minimum version we need it to be for our plugin to work.

First, we need to create a function and setup some variables…

function bcd_requires_wordpress_version() {
	global $wp_version;
	$plugin = plugin_basename( __FILE__ );
	$plugin_data = get_plugin_data( __FILE__, false );
	$require_wp = "3.5";

line 2 sets up the WordPress global variable $wp_version. This global variable returns the installed version of WordPress. If you are curious you can visit the WordPress codex for a list of more WordPress global variables.
line 3 gives us the basename of our plugin which will be used to shut it down later
line 4 gives us some meta data about our plugin so we can display a more useful message to the user
line 5 sets the bar for what minimum version we need in order for our plugin to work.

Now that we have our variables we can do some comparing and shut down the plugin if the criteria is not met.

 
	if ( version_compare( $wp_version, $require_wp, "<" ) ) {
		if( is_plugin_active($plugin) ) {
			deactivate_plugins( $plugin );
			wp_die( "<strong>".$plugin_data['Name']."</strong> requires <strong>WordPress ".$require_wp."</strong> or higher, and has been deactivated! Please upgrade WordPress and try again.<br /><br />Back to the WordPress <a href='".get_admin_url(null, 'plugins.php')."'>Plugins page</a>." );
		}
	}
}

line 1 makes a call to a PHP function which compares two “PHP-standardized” version number strings.
line 2 checks to see if the plugin is activated before it bothers to shut it down
line 3 deactivates the plugin
line 4 stops further processing and displays a message to the user about deactivating the plugin

Last, but definitely not least, we need to hook into our new function. During the admin_init is the best time to check. This will ensure the plugin is deactivated before someone can try and use it.

add_action( 'admin_init', 'bcd_requires_wordpress_version' );

Example 2: Plugin depends on another plugin

In this example we will be deactivating our plugin if another plugin is not activated. This is a handy thing to do if you have a written a plugin that depends on another plugin.

The first thing we will need to do is create a function and setup our variables.

function bcd_requires_bcd_plugin() {
	$plugin_bcd_plugin = 'bcd-plugin/bcd-plugin.php';
	$plugin = plugin_basename( __FILE__ );
	$plugin_data = get_plugin_data( __FILE__, false );

line 2 is the plugin that needs to be active for our plugin to work
line 3 gives us the basename of our plugin which will be used to shut it down later
line 4 gives us some meta data about our plugin so we can display a more useful message to the user

Next we will check to see if our required plugin is active and deactivate ours if it is not.

	if ( !is_plugin_active( $plugin_bcd_plugin) ) {
		deactivate_plugins ( $plugin );
		wp_die( '<strong>' . $plugin_data['Name'] . '</strong> requires <strong>BCD Plugin</strong> and has been deactivated! Please activate <strong>BCD Plugin</strong> and try again.<br /><br />Back to the WordPress <a href="' . get_admin_url( null, 'plugins.php' ) . '">Plugins page</a>.' );
	}
}

line 1 checks to see if the required plugin is activated before we bother to shut ours down
line 2 deactivates our plugin
line 3 stops further processing and displays a message to the user about deactivating our plugin

Just as in our first example we need to hook into our new function. And, just like our first example the admin_init is the best time to check because this will ensure the plugin is deactivated before someone can try and use it.

add_action( 'admin_init', 'bcd_requires_bcd_plugin' );

Summary

As you can see it is a fairly simple thing to shut down a plugin and it is a pretty powerful tool to have at your fingertips. Just remember…with great power comes great responsibility.

References

$wp_version
deactivate_plugins
get_plugin_data
is_plugin_active
plugin_basename
version_compare
wp_die

Posted in Tech Tagged with: , ,

Leave a Reply