WordPress Plugin Dependency Checking
July 27, 2009 – 1:07 pm
I use the excellent NextGen Gallery to manage the images in several web sites. But it uses javascript to display the individual full-size images, and that’s no use for search engine optimization, so i’m writing a plugin to produce a templateable HTML front end for it.
That plugin obviously requires NextGen Gallery to be installed – or, at least, for its database tables to be available – but WordPress’s plugin activation system doesn’t have any built-in way of checking dependencies.
I tried testing for one of NGG’s tables in the activation function of my plugin, but i couldn’t get that to work, as there doesn’t seem to be a mechanism for plugin activation to fail gracefully. So i did a bit of google searching and turned up a possible solution at http://fullthrottledevelopment.com/creating-dependent-wordpress-plugins.
Their solution uses a function which runs every time plugins.php is loaded, which checks to see if the plugin is being activated and, effectively, deactivates it with an error message. That half worked for me, but it didn’t prevent the code in my plugin’s activation function from being run (it creates a page and registers some permalink rules). That leaves plugin activation stuff done but the plugin not activated – which isn’t good.
So i tried moving the plugin activation code into the dependency check function – but it wouldn’t work there and i couldn’t quite manage to work out why. Then i tried moving the dependency check code into the plugin activation function – but that didn’t work properly either.
I was just about to give up and not bother with dependency checking when i came across a blog post called “I’ve made a useless WordPress plugin…” by Simon Wheatley. He’s checking for PHP version, but the principle’s the same.
Simon’s solution is so simple that i can’t see why you’d need anything more complicated. Just check dependencies in the top of the plugin file and die with an error message if they’re not met. None of the other code in the file will ever be executed if this dependency check fails – so there’s no half done activation to worry about.
This was the code i ended up using:
global $wpdb;
$sql = "SHOW TABLES LIKE '{$wpdb->prefix}ngg_gallery'";
$results = $wpdb->get_results( $sql );
if ( ! $wpdb->num_rows )
{
die( '<p>This plugin will not work unless NextGen Gallery is installed and activated.</p>' );
}
Hey, I found this post as I was searching for “how to figure out if a plugin is installed”. You have a great quick solution, but unfortunately I have found that many WP plugins are not good about cleaning up after themselves…
in the case where a user has at one point installed NextGen Gallery (or in my case, WP e-Commerce Plugin), but gave up on it and uninstalled/deleted it, the db tables often might not get removed correctly, and your test would show that the plugin is there when it really isnt any more.
Just implemented something very similar. Was hoping for something built into WP for this, but found nothing. This is the best solution I’ve found.
A couple minor improvements to the usability: Instead of die(), just return. This will stop execution of the file, but won’t kill the entire wordpress stack.
To notify the user add an admin notice:
if (/*PLUGIN-NOT-INSTALLED*/) {
add_action(‘admin_notices’, ‘dependency_error’);
return;
}
function dependency_error () {
echo ‘Required dependency is missing.’;
}
Interesting and useful. Thanks for that!
WordPress keeps track of active plugins in the `options` table.
SELECT * FROM wp_options WHERE option_name = ‘active_plugins’;
WordPress keeps track of active plugins in the `options` table.
SELECT * FROM wp_options WHERE option_name = ‘active_plugins’;
global $wpdb;
$active_plugins = get_option(‘active_plugins’, FALSE);
$depends = ‘nextgen-gallery/nggallery.php’;
if ( ! in_array($depends, $active_plugins)) {
die(“Required plugin, {$depends} could not be found, please activate it before continuing”);
}