Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

Best Practice: Check if Function Exists When Adding in WordPress Theme

Last updated on by
Follow WPBeginner on YouTube
Best Practice: Check if Function Exists When Adding in WordPress Theme

The best part about WordPress is plugins. Theme designers also have a tendency of using WordPress plugins for adding cool features. When working on a custom WordPress theme project for a client, one thing you want to make sure is follow the best practice. Yesterday, we got an inquiry from one of our users whose site was broken. He had a custom design done by someone. The theme worked great until he changed hosts. His single posts would not load after post content. The comments area, sidebar, footer, nothing will load. It will simply output the error. We went in and fixed the issue for them. The issue was that his theme designer did not follow the best practice. He had a very popular plugin “User Photo” which allows you to add user’s photo at the bottom of the post, except he was just adding the function directly. Now this would work unless ofcourse the plugin gets disabled for some reason. When this client switched hosts, apparently the GD Library was not installed in the new server. This caused the plugin to deactivate. You also could not re-activate the plugin because it relies on the GD Library. This caused the site to be broken for all users. This obviously gets that theme designer a bad reputation because when the client asked what was the issue, we explained to them. If you are a theme designer adding plugin outputs, you should always follow the best practice. Here are some examples of how we do things with our clients:

For the popular User Photo plugin, we have it like this:

<?php
if( function_exists('userphoto') && userphoto_exists($author->ID))
    userphoto($author->ID);
else
    echo get_avatar($author->ID);
?>

The code above checks for two things. It checks if the ‘userphoto’ function exists (basically if the plugin is active). The second check is to see if the user photo exists for the specific author. If both checks return true, then we display the userphoto. Otherwise we simply have it displaying the user’s Gravatar.

For other plugins, which we do not have a substitute for, we always add a little note. For example when we add OIO Publisher output:

<?php if(function_exists('oiopub_banner_zone')) {
oiopub_banner_zone(1, 'center');
} else {
echo 'OIO Zone 1 does not exist. Check to see if this plugin is active.';
}
 ?>

The code above basically see if OIO Publisher Banner Zone function exists (which it will unless the plugin is deactivated). If it does exist, then it will output the banner. If it doesn’t exist, then it shows the text to let the site owner know that this plugin has been deactivated.

If you do not add the function_exists, then your site will return an error where the function fails. Theme designers please start doing this.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • Joana Pereira

    Good call Kovshenin. I know exactly what you mean because I was using a custom function with contact form 7 (to retrieve the ip address on each form) and every time the plugin was updated, the theme broke.

    Thanks for the tip

    Joana Pereira

  • kovshenin

    Right, only please stop checking for dynamic_sidebar with function_exists in WordPress themes, unless you need to support WordPress 2.2 and below, which I *highly* doubt. Also with the user photo plugin, the whole approach seems to be wrong and redundant to me, it looks like many developers are missing the whole point of pluggable functions…. Oh well :)