You can’t assume everyone is running the latest version of WordPress. If your plugin or theme code relies on specific WordPress features (especially newer ones), you may need to check the which version of WordPress the site is running on.
Simple Version Check
There are two ways to get the WordPress version:
get_bloginfo( 'version' );
Or, with the global variable $wp_version:
global $wp_version;
For instance, to check if the site has WordPress 4.3 or later installed, you could use this:
[gist id=”a3e916d5efddad1091c9c5e9edd4255f” file=”wordpress-version-compare.php” lines=”5-8″]
More reference is here: http://codex.wordpress.org/Function_Reference/get_bloginfo
Use a function
If you’re needing to compare WordPress versions multiple times in your plugin, you may want to consider using a small function:
[gist id=”a3e916d5efddad1091c9c5e9edd4255f” file=”wordpress-version-compare.php” lines=”14-24″]
Use function_exists
Another easy way to find the version is to check for a function that was introduced after a certain release. For instance, post formats were introduced in 3.1. So to see if it 3.1 or higher you could use something like:
[gist id=”a3e916d5efddad1091c9c5e9edd4255f” file=”wordpress-version-compare.php” lines=”10-12″]
I actually use this function:
if ( ! function_exists( ‘is_version’ ) ) {
function is_version( $version = ‘3.0’ ) {
global $wp_version;
if ( version_compare( $wp_version, $version, ‘<' ) ) {
return false;
}
return true;
}
}
Cool. I added it to the post. Any reason to use this over the basic check?
No not really. Just easier for me to create the function and call
is_version()
throughout the plugin or theme files.Or you can check the $wp_version variable – the get_bloginfo(‘version’) function simply returns this variable.
Good point! I updated the post a bit.
Thanks for this. Here is my function that checks if WP is at least at a specific version ($version). Useful if you want to enable some features only for newer versions of WP.
/**
* Checks if WordPress is at least at version $version
* @param $version
* @return bool
*/
function wordpress_is_at_least_version($version)
{
if (version_compare($version, get_bloginfo('version'), '>=' ))
return true;
else
return false;
}
Usage:
if(wordpress_is_at_least_version(‘3.8’)) { //do something };
I’ve come back to this page quite a few times in the past few years. I think I’ll update you with my latest function.
public static function is_version( $operator = ‘<', $version = '4.0' ) {
global $wp_version;
return version_compare( $wp_version, $version, $operator );
}
Note the use of static, this should be within a class
Thanks Austin!