Backwards Compatible WordPress 3.0 Functions

Backwards Compatible WordPress 3.0 Functions

Posted on 21. May, 2010 by Alex Denning in Theme Development

We all know that WordPress 3.0 is coming and there are a whole plethora of new features, but actually adding them to themes? I had to do that today in an update to WPShift‘s ShiftNews. Trouble was I wanted to ensure the theme remained compatible with 2.9 and below, which meant I needed some good ol’ conditionals. In this post we’ll find out how to make backwards compatible 3.0 functions.

The problem

You’ve probably heard the “you just need to add one line!!” going around about activating the exciting new features of WordPress 3.0. Whilst that’s true, that is going to mean that your theme requires WordPress 3.0 or above to be run as below this the function won’t be recognised and the page just won’t load. If you’re a responsible theme maker, you know that’d be bad and so obviously want to do something about it to ensure your themes run properly in any of the more recent version of WordPress.

The solution

As I mentioned, the solution is to throw conditionals at the function, in much the same way you’d do it for plugins. The advantage of this is that you can offer a fallback for users who don’t have 3.0 installed; for the menus, for example, you can fall back on list_categories.

Functions

First off in your functions.php file wrap each function in

if (function_exists('function-name')) {
  1.     add_theme_support('function-name');
  2. }

For example, for menus it’d be:

if (function_exists('nav-menus')) {
  1.     add_theme_support('nav-menus');
  2. }

Or for custom backgrounds:

if (function_exists('add_custom_background')) {
  1.  add_custom_background();
  2. }

And repeat for each function. I did try combining conditionals but that just presented me with a blank screen; if anyone has a solution do let me know.

In the theme

For custom backgrounds that’s all you need to do but for menus, for example, there’s more to do: you’ve got to add the wp_nav_menu function where you want the menu to show up. You’ll need another conditional, this time with an else to provide an alternative for previous versions:

<?php if ( function_exists('wp_nav_menu') ) { //if 3.0 menus exist
  1. wp_nav_menu(array('menu' => 'Header') ); }
  2.  
  3. else {?>
  4.  
  5. <ul><?php wp_list_categories('title_li='); ?></ul>
  6.  
  7. <?php } ?>

It’s that simple

Really, it is. It’s one of those things that you should do to make your theme as compatible as is reasonable (and I think supporting 2.9 is reasonable when the 3.0 upgrade is one that some might wait on because of plugin incompatibilities) and now it’s easy for you to do so there’s no excuse!

No related posts.

Follow on Twitter! Subscribe!
Alex's Gravatar

Alex Denning is the founder of WPShout. A WordPress developer from London, Alex co-founded WPShift at the start of 2010 where he sells awesome WordPress themes.

You can find Alex on Twitter and at AlexDenning.com.

9 Responses to “Backwards Compatible WordPress 3.0 Functions”

  1. Amor

    22. May, 2010

    Very useful post, as usual. You’re absolutely right, making it backwards compatible is easy just by using the function_exists condition, to check if it’s available before calling it. I’m testing WP 3.0 in my local WP install, added new functions without using conditionals. It’s working well of course, but I mistakenly uploaded the modified files to my server. Fatal error, ouch! Lesson learned: never take simple things for granted. :-)

    Reply to this comment
  2. Jared

    22. May, 2010

    Great tip. I was eventually going to think about this, just haven’t gotten to it since I’ve been so busy playing with all the new 3.0 coolness!

    Reply to this comment
  3. Jason Stephens

    24. May, 2010

    Thanks Alex, looking forward to full 3.0 release. Hopefully anytime soon…

    Reply to this comment
  4. chris

    24. May, 2010

    When I use the if statement for the nav menus in the functions.php, the menus tab says the “theme does not natively support menus” (even though it works fine when I create a menu). Is this just me or is this a bug? Are you getting this?

    Reply to this comment
  5. Bill Robbins

    04. Jun, 2010

    Great tips on making themes backwards compatible. There may be some benefit in using new functions to encourage end users to upgrade their installs. Just a thought.

    Reply to this comment
    • Alex Denning

      05. Jun, 2010

      In a couple of months, yes, but now whilst it makes some sense to hold back updating (and wait for 3.0.1 and the inevitable security fix), I’d say this is a better bet.

      Reply to this comment

Trackbacks/Pingbacks

  1. [...] Backwards Compatible WordPress 3.0 Functions – WPShout When upgrading your themes to 3.0, remember to keep them compatible with version 2.9! This post shows you how to do just that. [...]

Leave a Reply

Please use your real name when commenting. Wrap code in <code> tags and make sure HTML is encoded. You can use regular HTML like <a href="... etc.

Get yours questions answered quicker

If you're using this post for paid work and have a question of any complexity then please ask at WPQuestions where you'll get a prompt response.