WordPress 2.3 Theme Backward Compatibility

Over at BlogginPro, the question was posed:

My biggest question is “what will happen if we take the newly updated theme, and place it on a blog running a previous version of WordPress?”

Is there some way to check which version of WordPress is being run, or will we have to put out a WordPress 2.3 only version of each and every theme?

I’m not normally an active commenter on blogs, but since I knew the answer to the question, I figured I’d speak up and answer the question. Because of this, I figured I’d go ahead and answer it here as well.

PHP comes with the built in capability to check to see if a function exists. Since template tags are nothing more than PHP functions, it makes sense that you can very easily use the function_exists condition to check the availability of the function before actually calling it. It would go a little something like this:

<?php if ( function_exists('the_tags') ) {
the_tags('Tags:', ', ', ''); } ?>

So now, you have added the ability to add tags to your theme, without breaking it in all versions prior to 2.3.

Bonus Tip

OK, I have to admit that I haven’t actually done this for my themes, but I wanted to throw this out there as a possibility. If you’ve been developing themes for a while now, you’ve got to be aware of tags that have been deprecated. For instance, let’s say you wanted to list your categories in the sidebar. Normally, you would use the wp_list_categories function.

But if you wanted to be backward compatible, you would use the deprecated tag (wp_list_cats) instead of the new one. That way, you make sure you have a catch-all function (since WordPress doesn’t remove deprecated functions from the code for quite a while).

In reality, you’re using an inferior function, even if the user has a fully upgraded version of WordPress.

It would be better if you used the function check to make sure your theme is truly, fully backward compatible. That means that even after WordPress phases out the deprecated function, your theme still works.

This also eliminates the need to release different themes for different versions of WordPress. A single theme will work universally! Here’s a sample of how you would do it:

<?php if ( function_exists('wp_list_categories') ) { //if the new function exists
wp_list_categories(); } else {
wp_list_cats(); } ?>

Easy as cake! Enjoy your new, fully backward compatible theme!

Comments

  1. says

    I didn’t expect WP 2.3’s tagging functionality to cause problems with themes, but sure enough a theme I released last week has caused ‘undefined function’ havoc for some users. That if-statement is a good tip – thanks!