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.
- 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.
This post is part of the project: Popularity Contest. View the project timeline for more context on this post.
How to Safely Use Plugin Functions in a Theme…..visit: http://t.co/puZcNuSE
If you own the plugin yourself, then you can alternatively hook in the functionality, then call it via do_action( ‘bla’ ); in the theme. This avoids an extra line of code/simplifies the theme. Plus that hook might be useful for other things too.
I completely agree with Ryan here — action hooks are a much more elegant solution.
@Ryan – that’s what I was about to suggest. Even if you don’t own the plugin, I usually throw a bunch of add_action statements into my theme’s functions.php file for the plugin functionality I’m going to use. In general, I just use the name of the template tag as the action name, making it super easy to remember what is going to be output there.
How to Safely Use Plugin Functions in a Theme http://t.co/8RHAJR5U #Wordpress #WP #plugin #WordpressTheme #coding #Codex
I understand how a single
do_action()
call can seem more elegant, but architecturally you’re using the feature backwards.It depends on the context but actions are a safer bet in the WordPress world. I’d rather hook in once and not have to worry about that execution point ever having a problem, than littering code with a bunch of function_exists() checks.
It’s also possible that a plugin or theme is loading its code before some other plugin has had an opportunity to include its PHP libraries, which means a function_exists() check might fail only because of a load order issue, and not because that function won’t eventually exist later in the stack.
A better use for function_exists() is when building plugins and themes to work with older versions of WordPress. When new features are introduced like navigation menus and the theme customizer, it might be safer to check if a function exists before even hooking on to an action or filter.
As an example of why this is a bad habit, and how it lends towards confusing things in the long-term, the codex page for Widgetizing Themes still uses function_exists(‘dynamic_sidebar’), which has been in WordPress core since 2.2.
http://codex.wordpre[...]izing_Themes
When I see a function_exists() check in a WordPress plugin or theme, I’m immediately suspicious about why it’s there, since relying on actions and the WordPress core load order is more reliable, and easier to change later if you need to.
How to Safely Use Plugin Functions in a Theme http://t.co/1v0AvQSm #wordpresstip
How to Safely Use Plugin Functions in a Theme: http://t.co/j2oOfLUa
How to Safely Use Plugin Functions in a Theme http://t.co/2j4rMuJj via @alexkingorg