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.
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!
Lorelle
Wednesday 14 September
03:44 AM
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
Turnip
Wednesday 14 September
10:24 AM
In WordPress 1.6, you’ll be able to add a
functions.phpfile to your theme’s directory. Perhaps you could use this feature in the future? :)Mathias
Wednesday 14 September
09:32 PM
Thanks for this post. I search this for a long time. Someone try to do this in a “functions.php” file ?
obuisson
Tuesday 24 February
10:20 PM
Скажите, а можно ли взять какие-нибудь статьи с вашего блога? Со ссылкой на первоисточник конечно же.
hyclina
Friday 09 October
05:12 AM
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
Andrew
Tuesday 22 December
04:24 AM
big cheers Jonathan .. very nice tip
the_guv
Saturday 09 January
03:20 AM