How to safely implement WordPress Plugin template code

Add a Comment

This tip is fairly straightforward, but it’s absolutely necessary for those of us building and integrating Plugins into our themes. It’s important to do it the smart way. In this tutorial, I’ll show you how to add Plugins using template tags that won’t break your theme if your Plugin is broken or missing.

Making your Plugin implementation bulletproof

For any WordPress Plugin you come across there will usually be two ways of implementing it: using widgets and using the Plugin template code. What we’re interested in is keeping your theme from breaking down in case something goes wrong with a Plugin.

Assuming you use widgets you have nothing to worry about. If a Plugin breaks down the widget will just disappear. If you are using template tags, though, you’re in a different predicament.

That is, unless you use the following code when placing your template tag:

<?php
if (function_exists('FUNCTION NAME')) {
  FUNCTION_NAME();
}
?>

What this particular piece of PHP code does is first check to see whether or not the function being called exists (the Plugin) before calling it. In this way, you can build all sorts of support into your themes for Plugins that your users might end up wanting: breadcrumbs, Twitter, and advanced page navigation, for example.

If this sort of thing is up your alley, check out the page on the Codex where this little nugget came from.

Join in via the comments: what other tips do you use to keep your themes bulletproof?