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!

Alex Denning

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

Most Searched Articles

Best JavaScript Libraries and Frameworks: Try These 14 in 2024

In this post, we look at the best JavaScript libraries and frameworks to try out this year. Why? Well, with JavaScript being available in every web browser, this makes it the most accessible programming language of ...

25 Best Free WordPress Themes (Responsive, Mobile-Ready, Beautiful)

If you're looking for only the best free WordPress themes in the market for this year, then you're in the right place. We have more than enough such themes for you right ...

12 Best WordPress Hosting Providers of 2024 Compared and Tested

Looking for the best WordPress hosting that you can actually afford? We did the testing for you. Here are 10+ best hosts on the market ...

Handpicked Articles

How to Make a WordPress Website: Ultimate Guide for All Users – Beginners, Intermediate, Advanced

Many people wonder how to make a WordPress website. They’ve heard about WordPress, its incredible popularity, excellent features and designs, and now they want to join the pack and build a WordPress website of their own. So, where does one get ...

How to Start an Ecommerce Business: Ultimate Guide for 2024

Is this going to be the year you learn how to start an eCommerce business from scratch? You’re certainly in the right place! This guide will give you a roadmap to getting from 0 to a fully functional eCommerce business. ...