I recently wrote a new plugin, BNS Bio. Version 0.1 was released (and currently available on the WordPress Extend Plugins repository) as a teaching aide to be used with a presentation I was doing for the WordPress GTA Meetup group. Now, since this is version 0.1 of the plugin it is really not much more than well written “alpha-ware” and as such on the last dry-run of the presentation it was noted there is a bug that needs to be corrected.

An author details shortcode plugin with multiple extensions that can modify the output. The extension plugins, using some of the many available hooks, can add a rounded corner border; display the details as an unordered list; and/or hide the author email address details.
BNS Bio Description

Since this is an extensible plugin it made sense to have the extension plugins run a “sanity-check” conditional on whether or not BNS Bio was active. The version 0.1 code executed an exit() function call if BNS Bio was not active. Makes sense, right? The problem is if the extension plugin is active and BNS Bio is deactivated you could get locked out of the Administration Panels. No worries, the fix (version 0.2) is ready on GitHub and will be committed to the WordPress repository no later than November 20, 2012.

In the meantime, and the reason for this post, here is an example snippet if you need to self-deactivate a plugin based on whether or not another plugin is active:

/** @var $my_plugin_directory - define plugin directory name dynamically */
$my_plugin_directory = basename( dirname( __FILE__ ) );

/** Sanity check - is the plugin active? */
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active( $my_plugin_directory . '/my-plugin.php' ) ) {

    /** My truly amazing code that extends 'My Plugin' goes here */

} else {

    /** If 'My Plugin' is not active then self-deactivate 'My Plugin Extension' */
    deactivate_plugins( $my_plugin_directory . '/my-plugin-extension.php' );

}

Note the My Plugin Extension would be, in this case, located in the same folder as the extensible plugin with the above snippet of code included in the main My Plugin Extension file.