Skip to content

Backwards Compatible WordPress 3.0 Functions

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')) {
    add_theme_support('function-name');
}

For example, for menus it’d be:

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

Or for custom backgrounds:

if (function_exists('add_custom_background')) {
	add_custom_background();
}

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
wp_nav_menu(array('menu' => 'Header') ); }

else {?>

<ul><?php wp_list_categories('title_li='); ?></ul>

<?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!

Yay! 🎉 You made it to the end of the article!
Alex Denning
Share:

8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Wordpress 3.0 Roundup | Themergency
June 17, 2010 5:01 pm

[…] 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. […]

Google Hot Trends » WordPress 3.0 Features – Every Theme Developer Should Know
June 6, 2010 6:42 am

[…] Backwards Compatible WordPress 3.0 Functions […]

Bill Robbins
June 4, 2010 3:03 pm

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.

chris
May 24, 2010 11:25 pm

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?

Jason Stephens
May 24, 2010 10:07 am

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

Jared
May 22, 2010 4:33 pm

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!

Amor
May 22, 2010 4:41 am

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

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!