How to add backward compatibility to a WordPress 3.0 ready theme
The latest major release of our favorite blogging platform – WordPress 3.0 – has brought quite a few new features to the table. Among them, the custom navigation menus and the post thumbnails (featured image), the latter being available since version 2.9 came out. This started a whole new trend between theme developers, most of them rushing to announce WordPress 3.0 ready themes.
In all this rush, some of the themes released suffered from one big problem: backward compatibility. Why is that important and how can we give some backward compatibility to our WordPress 3.0 ready themes?
Importance of backward compatibility
While I feel that generally offering some sort of backward compatibility is just a common sense policy, in this case it’s more than that. Let me explain why.
Not everyone uses the latest WordPress release, and that is for various reasons:
- Failed to update, or waiting to make sure it’s “stable”;
- Doesn’t really get the importance of an up to date software;
- Certain plugins they use are not yet 3.0/3.0.1 compatible.
This doesn’t mean they don’t want to try and use your beautiful 3.0 ready WordPress theme, or that they shouldn’t. But you see, the biggest trick resides right here, in this terminology: “3.0 ready”. The actual meaning of this is that the theme is compatible with the 3.0 version, not that it requires 3.0 or higher to function properly, and this leads a lot of people into a trap that will cause their blogs and websites to break.
So, the common sense thing to do would be to take the time and apply certain checks and fallback solutions to our themes. Lets see how we can do that.
Adding WordPress 3.0 features with fallback support
Both of the above mentioned features – custom menus and post thumbnails – are based on a WordPress function added once 2.9 was released: add theme support();
Adding post thumbnails support
In order to add post thumbnail support to your theme, you’d write this in your functions.php file:
// Adds support for post thumbnails
add_theme_support('post-thumbnails');
Simple adding this will cause any WordPress installation prior to 2.9 to break, as the add_theme_support() function was not part of the WordPress core back then. The fix? As easy as this:
/* Checks if the installation has the function defined.
If true, continues to add the support for post thumbnails. */
if(function_exists(add_theme_support)) :
add_theme_support('post-thumbnails');
endif;
Adding custom menus support
In order to also add support for the custom menus feature, you’d normally write this:
// Add theme support for custom menus add_theme_support( 'menus' ); // Register a custom menu name/location register_nav_menus(array( 'custom-menu' => __( 'My Custom Menu' ), ));
In this case, we obviously need to check for core support of both functions: add_theme_support() and register_nav_menus(). Let see how we can do that, also including in this check the post thumbnails:
if(function_exists(add_theme_support)) {
add_theme_support('post-thumbnails'); // Support for post thumbnails
if(function_exists(add_theme_support)) {
add_theme_support( 'menus' ); // Support for custom menus
register_nav_menus(array(
'custom-menu' => __( 'My Custom Menu' ),
));
}
}
In case you wonder why I chose to add support for menus only after checking if register_nav_menus() is defined, it’s simply because there’s no point in adding support for a feature that you won’t be able to use. Remember, add_theme_support() was added in 2.9, while register_nav_menus() became available only in 3.0.
But how about adding compatibility in the front-end of your theme?
When using post thumbnails, never call them without checking if the function exists first:
// Performs a check to see if the function is available and there's also a thumbnail attached
if (function_exists(the_post_thumbnail) AND has_post_thumbnail()) {
the_post_thumbnail();
}
For the custom menus, the thing is just a tad trickier, but simple as well. I’m usually using a function that I wrote to specifically check for custom menus support, and even though the wp_nav_menu() function provides a fallback solution, defaulted to wp_page_menu(), this fallback will only activate for 3.0 installations without any menus created and allocated to the reserved space. The fallback won’t set off if you’re using a version prior to 3.0.
Due to my preference of wp_list_pages() over wp_page_menu() – for more customization options – the following function will apply a compatibility fallback to wp_list_pages:
function my_nav_menu() {
// Check if installation has wp_nav_menu() defined.
// If true, generate the menu, but don't print it yet;
if(function_exists(wp_nav_menu)) {
$my_nav_menu = wp_nav_menu(array('menu' => 'Top Menu', 'container' => '', 'fallback_cb' => 'wp_list_pages', 'depth' => 2, 'echo' => false));
}
// If false, generate the menu using wp_list_pages, but don't print it yet;
else {
$my_nav_menu = '<ul class="menu">'.wp_list_pages('sort_column=menu_order&title_li=&echo=0&depth=2').'</ul>';
}
// Prints out the menu;
echo $my_nav_menu;
}
Now, in the front end. instead of calling your menu with wp_nav_menu() you’ll now use my_nav_menu() to display a backward compatible version.
Conclusions
Caught in the rush of doing something nice and new, like a child discovering a shiny new toy, we can miss out some of the most basic problems. Most of them are really easy to solve, like the above mentioned and it would be common sense to make use of them.
And remember: if ( function_exists(function_name) ) is your geeky B.F.F.
-
- Spread the love!
- Digg
- StumbleUpon
- Delicious
- Free, fast RSS updates?
- Join the 2,141 subscribers NOW!

Great article but it is rather harder for non-tech blogger like me. I usually ask my designer to take care of this. Thanks anyway
Thanks a lot for sharing this code for making a theme backwards compatible. But I think everybody should upgrade to WordPress 3.0 now cos it really has some cool features
Tinh, the post is dedicated to theme designers. Others might find it hard to follow indeed.
Other commentator: What people should do, does not always happen in reality. As said in the post, various people have various reasons. Some might just install the theme without updating first, wishing to update after, but that will cause the site to break, and they won’t have a chance to update later, unless deleting the whole theme folder. That’s why a few lines of extra code would not only be common sense, but also save some time in bad situations. And common sense would also be to use a name, and not a keyword when commenting.
Yes, Alex, I agree with the others. It’s too technical. Although I appreciate how you explain thoroughly on how to get around a compatibility problem, lots of WP themes provide updates anyway. I did have a personal experience with this type of problem when I changed my theme about two days ago. My blog just crashed. I thought I lost it. It’s so easy to make a mistake in the coding (I’m still learning here).
Nice tips..to setup a theme for a blog!
Jun, I’ve posted “technical” entries before. They are meant for those people looking for them, and Blogsessive readers know about my love for WordPress and skills in development. These posts are targeted at people with some knowledge in this area, as said: theme designer/developers. Also, the themes you mention are usually paid themes that come with support and upgrades. With free themes you do not get that commodity, and trust me, the percentage of people using free themes (outdated or not) is WAY higher than that of people using premium themes or freemium themes (premium quality free themes).
Thanks for the info here. I just started using wordpress and this will help me a lot. WordPress is really nice. Very easy to use.
Hi Alex,
Do you think you would be able to add 3.0 wordpress support for Custom menu especially in your lovely Simple Balance 2.2 theme?
Would really love to use it…. with the custom menu support.
Thank you.
Faustine, yes, I’m planning on releasing an updated Simple Balance with more features included, among theme custom menus and custom backgrounds.
Hi Alex,
Its been some time ever since Aug 10 when I last contacted you with regards to the update for Simple Balance “3.0″..
Any updates about the release?
Your simple Balance theme is a wonderful perfect theme for me, due to the wide fluid layout and simple customisations…
Would love to have the Custom Menus up so that I can link to my eshop…
Thank you.