November Echo Romeo Delta
a blog by Will Kemp

  • Recent Posts
    • Web Directions South 2009
    • Fedora 11 on a Samsung N140
    • Wordpress Plugin Dependency Checking
    • Nokia 5800 Touch Screen Smartphone
    • Moving From Drupal Back To Wordpress
    • Making A Wordpress Theme
    • Object Oriented PHP With CodeIgniter
    • Optimising Ajax Search Suggestions
    • Passing Variables From PHP To Javascript
    • Ajax Search Suggestions Dropdown List
    • Subversion Hell
    • Converting From Wordpress To Drupal
    • Moving A Wordress Site To A New Server
    • Shrinking VirtualBox VDI Files
    • Dynamic XML Sitemap Using PHP
    • Older Posts
  • Other
    • About Me
    • Contact
  • Misc
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org

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>' );
}

Leave a Reply

Click here to cancel reply.

© 2008 Will Kemp. All rights reserved.
Design: Wordpress Classic theme, modified by Will Kemp