Using WordPress Template Tags Safely

Have you ever downloaded a WordPress theme that had support for a specific plugin built-in? When you installed the plugin the theme recognized that the plugin was activated and adjusted itself to accommodate. If you were to deactivate the plugin, the theme would gracefully ignore the plugin’s template tag instead of exiting with an error message.

How is this done? How can you use a plugin’s template tags and not have to worry about fatal errors if you remove the plugin and forget to take the tags out of your theme? You use the handy PHP function known as function_exists().

Let’s use WP125 as an example. Instead of putting <?php wp125_write_ads() ?> where you want the ads to display, you put this:

<?php if ( function_exists('wp125_write_ads') ) { wp125_write_ads() } ?>

It looks a little messier, but it will prevent your blog from breaking if you accidentally deactivate the plugin without first removing the template tag. Of course, in the case of WP125 you could simply use widgets instead…but that wouldn’t be as much fun. :)