WordPress: Plugin dependencies

One of the things I thought was lacking from the WordPress theming system was the ability for themes to depend on plugins (or maybe even plugins depend on each other). In my redesign I have abstracted all “fiddly” code into plugins, and I’m also utilizing a number of 3rd party plugins too. Without these plugins my theme would fall flat on its face, so I devised a mechnism to ensure that I always had the required plugins installed and activated—otherwise the page doesn’t display at all. In the top of my themes header.php I have:

require(TEMPLATEPATH . '/plugindeps.php');

The file plugindeps.php then contains:

<?php
$required_plugins = array(
    'plugin_fn1',
    'plugin_fn2',
    'plugin_fn3'
);
$missing = array();
foreach ($required_plugins as $plugin)
    if (!function_exists($plugin) && !class_exists($plugin))
        $missing[] = $plugin;

if (!empty($missing)) {
    header('Content-Type: text/plain');
    foreach ($missing as $plugin)
        echo "Required plugin '$plugin' is missing\r\n";
    die();
}
?>

Into the $required_plugins array goes a list of at least one function or class from each required plugin and you are set. That’s it.

Posted on Tuesday, September 13, 2005.

7 Comments on WordPress: Plugin dependencies

  1. This is great, though there is some strange things happening on your site when entering comments – so you might want to look into that.

    Anyway, this is brilliant. Have you considered contributing the information to the WordPress Codex. I bet it would help out plenty of users. If you are unfamiliar with the Codex, just read the instructions from the Community Portal link in the sidebar on how to contribute. Then a lot of people could benefit from your brilliance. Thanks!

  2. Thanks for the complements : ). I’ve added it to the Codex as a link here, is that okay?

    Regarding the comment form, this site is a mess at the moment, but I’m slowly working towards launching a design I made literally months ago. I’ve been quite busy :P

  3. In WordPress 1.6, you’ll be able to add a functions.php file to your theme’s directory. Perhaps you could use this feature in the future? :)

  4. Thanks for this post. I search this for a long time. Someone try to do this in a “functions.php” file ?

  5. Скажите, а можно ли взять какие-нибудь статьи с вашего блога? Со ссылкой на первоисточник конечно же.

  6. Thanks for this post, do you know if it possible for a theme to turn off or turn on a plugin. I want to have a development theme that turns on WP-Tuner and a couple of other plugins.

    Any idea if there is a way to activate or deactivate a plugin from a theme

  7. big cheers Jonathan .. very nice tip

Post a comment

You may use Textile for formatting.