Several plugins provide users with template tags like functions to include plugin output into themes, the most common way of including plugin code into themes looks like this:
<h2>Section Header</h2>
<?php plugin_template_tag_function(); ?>
Though the above code is absolutely right, PHP errors will occur if you disable the plugin and do not remove the template tag from the theme. There is a much better way of including template tags in your themes, which ensures that PHP errors do not occur when you disable plugins and do not remove them from your themes:
<?php if ( function_exists(‘plugin_template_tag_function’) ) : ?>
<h2>Section Header</h2>
<?php plugin_template_tag_function(); ?>
<?php endif; ?>
The if condition in the above code ensures that the function you want to use is registered, before the code is executed. This extra check will ensure that your theme will load without PHP errors, even if the plugin has been disabled.
If you are new to conditional statements, you can read one of our earlier post about if, then and else conditions.
Is there a way to check and see if that plug-in is active?
Daniel you can do that from the admin panel on the plugins page.
Yes, you can. IF you can check to see if the plug in is active, not just that function though, you can set up an if statement at the start and set variable to true or false, then a tertiary(?) if to decide rather something is written or not.
Yes there is use:
$current_plugins = get_option(‘active_plugins’);
if (in_array(‘plugindir/pluginfile.php’, $current_plugins)) {
// The plugin is active
}
Thank you.
But this is an inefficient way of doing it and not necessary…inefficient as you are doing a get_option and scanning through an array…
I would say it breaks even personally. You scan the array to find it, set a session or global variable and have it use that on the page. Now if you have 15 plug-ins and your doing it to I can see where that could be a burden.
Thanks Keith for putting together this post. This seems to showcase a better example of what I tried to explain within that if, then, else post.
A better way is to just add some hooks to your theme. That way you create a generic API rather than tying your theme to a specific plugin.
I think this is much better way to embed some plugin code in theme. It is easily to understand. Thanks for the great knowledge.
Benedict is absolutely right. By far the best way to achieve this is to add custom hooks and actions to your theme. That way you can add multiple actions to each hook and the code is very much leaner than having if…else loops all over the place.
The whole of the example code in this article could be rewritten with:
<?php do_action( 'my_theme_action' ); ?>
and then add an action to your functions.php file like this:
<?php add_action( 'my_theme_action', 'my_theme_function' ); ?>
and then use my_theme_function() to output the desired code.
johnbillion’s method is outlined in a bit more detail here.
That would involve users having to manually add in hooks to all the plugins, which would automagically disaapear when you apply updates from plugin authors.
On the other there are times where hooks into content do not give desisred outputs, for example I use a ratings plugin, but do not want it to show up at the end of the post, I want it to be a part of the post meta, this would mean I have to include the plugin template tag in my theme, this approach safeguards me from generating PHP errors, if for some reason I decide to deactivate the plugin and forget removing the template tag from the theme.
Not true. The idea of the hook is that if its already in the theme, then users would hook the template tag from the plugin (which normally would get pasted into the theme) into the theme via functions.php.
The only thing missing is teaching new users how this is done, since most plugins with template tags talk about pasting them in the theme.
Even better if the plugin author actually built custom actions into their plugins. So all you would have to type in the template is do_action(‘theplugins_custom_hook’);
And if the plugin was disabled then nothing would happen.
@Keith:
That’s not the case at all. Instead of adding
< some_plugin_function(); ?>
into your theme, you’d add< do_action( 'my_action' ); ?> and then in your theme's functions.php file you'd add
< add_action( 'my_action', 'some_plugin_function' ); >
(or, if the function requires parameters, you'd use a wrapper function).I hadn’t thought of that — adding it the the theme instead of the plugin. Nice. 🙂
As Barry discovered, the easiest is for plugin authors to build the custom actions into their plugins…
True. Note that the article I linked above was targeted at plugin authors, not end users.
I do, however, like the concept of adding an add_action to a theme’s functions.php file if you do want to do it as an end user.
Can you tell me why or do you see any specific benefits or advantage?
Why should the user take the trouble of adding those hooks to theme’s functions.php file?
Oops, mucked my formatting up a bit there. The point is, you do not have to modify any plugin files.
No, I am saying the plugin author should add them as he needs those hooks for his plugin to generate the desired output…
Very useful information in this post, thanks.
This post is a brilliant example of various php developers brainstorming different alternatives to code and trying to arrive at the best way of doing things…though these are simple code, the post really helps to read the minds of different developers…great post and continue these sort of posts here… 🙂