How To: Add plugin function calls without breaking your theme

Haven’t you come across some meaningless pile of text, after which nothing is displayed? It breaks down the whole theme, causing problems for visitors and search bots alike.

Don’t we all hate those error codes?

What are function calls?

Function calls are bits of PHP code that looks like this:<?php ns_show_top_commentators(); ?>
Most plugins have such tags for specific uses; the above-mentioned code is to list the top commentators using the Show Top Commentators plugin.

The reason

PHP function calls are notorious for breaking the theme, if the requested function does not exist. This usually happens when you add a function call for a plugin that you have disabled. The result will be unaccessible blogs, which are caused by the behaviour of broken function calls. WordPress cuts off the rendering of page when it stumbles over a broken function all. Thus, depending on where you placed the broken code(sidebar/header/footer), your content may not be displayed, sidebars hidden or footers gone awry.

The solution

This problem can be overcome by using an if function_exists check. What does it do? It checks whether the given function actually exists, and if it does, proceeds to call the function. This way, your theme will work even when the function call is broken.

Let us see an example of the conditional code (for Alex King’s Twitter Tools plugin, used here on the top navbar).

The normal code for the Twitter tools is this:
<?php aktt_latest_tweet(); ?>

The conditional code would be this:
<?php if(function_exists(’aktt_latest_tweet‘)) { aktt_latest_tweet(); } ?>

To use it for any other plugin, find and copy the what you find in place of aktt_latest_tweet in the first code block. Then, paste that part in places of the code in red in the second code block.

This is a bit tricky, but play with it in a test blog installed locally on your PC, and once you get it right, upload it to your server. Feel free to leave a comment on any issues that crop up on the way.

Leave a Reply

© Techzilo 2020 All Rights Reserved.