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.
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:1.
// Adds support for post thumbnails
2.
add_theme_support(
'post-thumbnails'
);
1.
/* Checks if the installation has the function defined.
2.
If true, continues to add the support for post thumbnails. */
3.
if
(function_exists(add_theme_support)) :
4.
add_theme_support(
'post-thumbnails'
);
5.
endif
;
Adding custom menus support
In order to also add support for the custom menus feature, you’d normally write this:1.
// Add theme support for custom menus
2.
add_theme_support(
'menus'
);
3.
4.
// Register a custom menu name/location
5.
register_nav_menus(
array
(
6.
'custom-menu'
=> __(
'My Custom Menu'
),
7.
));
01.
if
(function_exists(add_theme_support)) {
02.
add_theme_support(
'post-thumbnails'
);
// Support for post thumbnails
03.
04.
if
(function_exists(add_theme_support)) {
05.
add_theme_support(
'menus'
);
// Support for custom menus
06.
register_nav_menus(
array
(
07.
'custom-menu'
=> __(
'My Custom Menu'
),
08.
));
09.
}
10.
}
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:
1.
// Performs a check to see if the function is available and there's also a thumbnail attached
2.
if
(function_exists(the_post_thumbnail) AND has_post_thumbnail()) {
3.
the_post_thumbnail();
4.
}
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:
01.
function
my_nav_menu() {
02.
// Check if installation has wp_nav_menu() defined.
03.
// If true, generate the menu, but don't print it yet;
04.
if
(function_exists(wp_nav_menu)) {
05.
$my_nav_menu
= wp_nav_menu(
array
(
'menu'
=>
'Top Menu'
,
'container'
=>
''
,
'fallback_cb'
=>
'wp_list_pages'
,
'depth'
=> 2,
'echo'
=> false));
06.
}
07.
// If false, generate the menu using wp_list_pages, but don't print it yet;
08.
else
{
09.
$my_nav_menu
=
'<ul class="menu">'
.wp_list_pages(
'sort_column=menu_order&title_li=&echo=0&depth=2'
).
'</ul>'
;
10.
}
11.
// Prints out the menu;
12.
echo
$my_nav_menu
;
13.
}
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.
Comments
0 Response to 'How to add backward compatibility to a WordPress 3.0 ready theme'
Post a Comment