How to Safely Use Plugin Functions in a Theme

I feel like I’d be remiss if I didn’t add this to the conversation. I’ve seen some people indicating that calling plugin functions in themes is a bad idea (here’s an example comment to that effect). I think this is going too far – including a call to a plugin in a theme is fine as long as you’re smart about it.

Basically, you want to make sure your theme still works if the plugin is no longer active. This means you make good decisions in your architecture, and are smart about how you call the plugin’s functions.

For example, instead of doing this in your theme code:

<?php
// Call to A Great Plugin
a_great_plugin_function();
?>

you’d want to do this instead:

<?php
// Call to A Great Plugin
if (function_exists('a_great_plugin_function')) {
    a_great_plugin_function();
}
?>

Now if the plugin is removed or disabled (or the function is removed in an upgrade) your site will handle that situation gracefully.

I’m a big believer in not reinventing the wheel. If a plugin has code you can use in your theme, use it.1 Just be smart about it.


  1. If you want to ensure the plugin is never disabled, you may want to consider including the plugin in your theme

This post is part of the project: Popularity Contest. View the project timeline for more context on this post.