How To: Add plugin function calls without breaking your theme

Howdy! It appears that you have come to this blog for the first time. In order to receive more useful posts like this, you can subscribe to RSS feed or receive latest articles to your email inbox. Thanks for visiting this blog, and have a good day.

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.

13 Comments
  1. 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

  2. Post authorSumesh said on October 13th, 2007

    @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)

  3. [...] Simple photo editing using gimp. 2. How to add a plug-in function calls without breaking your theme. 3. How to solve a [...]

  4. [...] Sumesh: How to Add Plugin Function Calls Without Breaking Your Theme [...]

  5. Who Needs Tutorials? Do I Hear Everybody? said on October 27th, 2007

    [...] Sumesh: How to Add Plugin Function Calls Without Breaking Your Theme [...]

  6. Who can’t use a couple of tutorials? said on October 27th, 2007

    [...] Kishore: How to Find Good Ideas and Content for Your Blog Adam: Adding a Blog to Flickr Sumesh: How to Add Plugin Function Calls Without Breaking Your Theme Simonne: How to Get Your Favorite Stumble Upon Pages in Your Feed Reader Shivaranjan: How to Move [...]

  7. Favourite WritersManifesto group writing project entries said on October 27th, 2007

    [...] had recently participated in WritersManifesto Group Writing project, with my entry, How to add plugin function calls without breaking your theme. I am supposed to post my favourite entries, so here [...]

  8. Andrew said on October 28th, 2007

    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.

  9. Post authorSumesh said on October 28th, 2007

    @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.

  10. Blogging Tutorial Group Writing Project - My Online World said on October 30th, 2007

    [...] Theme. Adam(moi) show how to Add a Blog to Flickr. Sumesh gets a little technical and shows How to Add Plugin Function Calls Without Breaking Your Theme. Simonne shows How to Get Your Favorite Stumble Upon Pages in Your Feed Reader, cool tip for [...]

  11. Blogging Tutorial Group Writing Project - My Online World said on October 30th, 2007

    [...] Theme. Adam(moi) shows how to Add a Blog to Flickr. Sumesh gets a little technical and shows How to Add Plugin Function Calls Without Breaking Your Theme. Simonne shows How to Get Your Favorite Stumble Upon Pages in Your Feed Reader, cool tip for [...]

  12. 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..

Leave a comment

  • Our network
  • India Technology Bloggers