Wordpress

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:

1 <?php
2 if( function_exists('userphoto') && userphoto_exists($author->ID))
3     userphoto($author->ID);
4 else
5     echo get_avatar($author->ID);
6 ?>

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:

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

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s