Handling error when WordPress plugin functions are not available

When we build a WordPress , sometime we need want to add more using plugin. For example, we want to display commenter’s avatar from Gravatar . Because we (as the theme creator) want to make our theme can handle Gravatar plugin by default, we directly put plugin function inside our theme file — to be more spesific inside comments.php.
If we share our theme (someone uses our theme), and his/her WordPress does not have plugin installed, the theme might be broken. There will be an . Single page (where the gravatar/comment should appear) will stop loading. Why? Because there is a gravatar plugin tag/function, but the plugin is not available.

Another thing, you downloaded a free theme, and you want to add gravatar function by yourself. So, you downloaded the Gravatar plugin, put plugin tag, and voila! All works. But when this plugin is deactivated (probably when you upgrade your WordPress installation) and you forgot to activate it again, your theme might nor working well. So, what’s the solution? It’s pretty simple: use the conditional tags. Okey, we’ll use Gravatar plugin as the example.

According to the documentation, you need to put " alt="" /> in your comment .
Sometime, we want to put it inside


so that the real output will be something like this:

Let’s have some modification. What we want to have is that when Gravatar plugin is not working properly or not yet installed, your theme will still work properly. So, we need to set a basic rule here:

  • Identify whether the plugin function is available or not.
  • If the function is available, display the avatar.

If we take a look at the example above, the plugin function is gravatar();. You can identify function existence using this code: , the whole new theme tag will be like this:

" alt="" />

So, if you a WordPress theme, you can write:

“This plugin will handle Gravatar plugin if available.”

rather than

This plugin support Gravatar plugin, if you do not want to display Gravatar, open your comments. file, and remove this line:

” alt=”” />

It is only an example, you can use the same method (taking advantage of if (function_exists(''))) for or other use.

3 replies on “Handling error when WordPress plugin functions are not available”

yeah , conditional tag can solve that problem, but I think WP had poor conditional tag feature.
Look , you can write conditional tag if plugins/ functions exist but you have not option to show something like error message or author announce that was related with plugins.
In TxP , we had conditional tags too, But with superb Improvisation and rich feature.
like :
<txp:if_plugin name=”cbs_gravatar” ver=”0.4> // trying search plugins… exist or not ..
<txp:cbs_gravatar /> // if exist , perform this tag …
<txp:else /> // if not …
<p>A message will iluminate if plugin cbs_gravatar is’nt exist // excecute this tag or text
</txp:if_plugin> // end of tag.
So …. Textish …

Well, actually, it does not have to be WordPress to have that if arguments. My example only shows the output IF the function exists, then the plugin displayed.
Of course, we can put else there. But, since in this case we do not need to show the “else”, I do not display the message.
WordPress, Textpattern or even Movable Type have this structure. if you can have <txp:else /> in Textpattern, you can use <MTElse /> in MT. In WordPress, just use <?php } else { ?> and <? } ?>

You right Ari, sometime we busy in design new layout for our template and decide to upgrade our “engine” to new one,but we forget to active our pluggin again. This conditional will help us.Thx