How To: Add plugin function calls without breaking your theme

ADVERTISEMENT

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.

ADVERTISEMENT

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.

Discussion

  1. Thanks..
    as i m not expert in php…
    so it helped me alot… and my problem is solved by reading ur post… thanks once again..

  2. @Andrew: Thanks. As a matter of fact, I hunted down this bug after I forgot to check my soccerblog.in, which had the error you see in screenshot above.

  3. That’s a great tip Sumesh. It can be really easy not to notice these errors at the time and come back a few days later to find errors where you least expect them.

  4. @Monika: Thanks. As regards PHP, neither did I – its just a matter of interest, time and elbow grease effort, plus some cheat sheets to learn PHP (hint: codex.wordpress.org)

  5. Hi Sumesh,

    Interesting post. I really love tech related stuff, as I can always learn more. PHP is one of my weak points as I never learned coding. Only trial by doing kind of stuff and reading about solutions like this is fascinating for me.

    Good luck with your entry.:-)

    Monika

Comments are closed.