Controlling the WordPress version installed inside your Themes

WordPress is a CMS that evolves rapidly. Currently, every four or five months there is a new version and this makes important to introduce in your developments some control to find out what is the installed version, especially if you want to use its latest introduced features and you want that your last development has backward compatible.

Controlling the WordPress version installed inside your Themes

Fortunately, WordPress makes it very easy because for not even you’ll have to use a function but there is a global variable called $wp_version that is always available in any part of your development that contains the number of the current version. See the first example.

<?php echo $wp_version; // Currently, this line outputs an string like '4.5.1' ?>

Very ease, but how to control is the current WordPress installed version is greater, equal or less than a particular version, for example, how do you know if the version is greater than 4.0? Because in most of cases the important thing is not to know what is the exact version but to find out if it is greater than… etc.

In that case we could construct a complicated function for making an string comparation but I suggest you another formula: to use a PHP function called version_compare. (See the version_compare oficial documentation). Using this function is possible to check easily any condition (greater, equal, less or equal, etc… ) about the current version because this standard PHP function accepts a parameter to decide what kind of comparation we want to do. For our last question about strictly greater than 4.0, the next code will work.

<?php 
if ( version_compare ( $wp_version, '4.0', '>' ) ) 
    return $a + $b;
else
    return $a - $b;
?>

This standard PHP function can be used with just the first two parameters, and in that case it returns (-1, 0, 1) depending on the condition strictly less, equal or strictly greater, or adding the third parameter, ie, specifying the condition and, in that case it returns a boolean ( TRUE or FALSE ).

A second approach to control what version is installed is the check if a specific function exist, for example, in the last WordPress version introduces a Theme Feature for adding a Logo to the themes so, if we check if a function related to this module exists then we’ll know it the WordPress version installed is greater or equal to ‘4.5’. To do this we have to use another PHP standard function called function_exists. (See the function_exists official documentation).

<?php
if ( function_exists( 'the_custom_logo' ) ) {
	// here the code for versions equal or grater than '4.5'
} else {
        // here the code for other cases
}
?>

I hope that these two simple examples can help you to develope a better and more robust themes.

Have a nice WordPressing!

Leave a Comment

   Mandatory field
You can use these HTML tags inside the commment.
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>