Version Testing for WordPress Theme Design.

wordpress version testingWordPress is (mainly) backwards-compatible in terms of its core code. But the code connected with themes or plugins that you add may well not be. For example, v2.9 has post thumbnails – put these in your new theme and they won’t work for older installs of WordPress – so some version testing is required.

There’s 2 ways to go about this:-

1. If you reckon your theme is no good without the features available to the latest version, you can prevent people from installing it in the first place – a bit brutal, but it does the job.

In functions.php, at the top:-


if(version_compare(get_bloginfo('version'), '2.8') < 0) die(__('This theme requires at least WordPress version 2.8','thistheme'));

More information about how PHP’s native function version_compare works here.

And then do tell people explicitly when they download the theme – compatible with WordPress versions > 2.8 etc.
In practice, some regularly manage not to read this, so stand by for complaint emails….

2. You can catch the errors before they happen – function_exists?

There’s a bit more work involved here. The guiding principle is to try to catch the errors as concisely as possible, rather than have a string of actions all wrapped up in their own if function_exists statements – and hopefully also offer some alternative to make sure that the degradation is as graceful as you can make it…

As a basic example, the file_get_contents function arrived in PHP 4.3 – and as of now, there are still servers around with older versions of PHP than this…

if function_exists('file_get_contents'){
//use it
$file = @file_get_contents($some_url);
} else {
// don't use it...
// and try to supply some alternative or warning
}

In the same area of grabbing a file, especially remotely, testing for curl is necessary…

if (function_exists('curl_open'))

You can also use:

if  (in_array ('curl', get_loaded_extensions()))

As an aside – WordPress comes with Snoopy bundled – custom-written to do the same job, but you do have to explicitly include it – so in a theme or plugin, the ‘official’ way to do it is something like,

require_once(ABSPATH.WPINC.'/class-snoopy.php');
$s = new Snoopy;

Lastly, and this is a very occasional thing – but since there are compiling options with PHP, (which modules are included and in which combination) a function name may exist even though the function itself can’t be used. Image functions associated with the GD extension can be a problem here, beware of functions like imagettfbbox(), imagefttext()

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • LinkedIn
  • Reddit
  • StumbleUpon
  • RSS
  • Twitter

Leave a Reply