is_wp_version_compatible()
Compares the provided version of WordPress with the installed version of WordPress.
Works based on the function version_compare() with the condition >=.
See also the similar PHP version comparison: is_php_version_compatible().
Used By: validate_plugin_requirements()
1 time — 0.000001 sec (speed of light) | 50000 times — 0.03 sec (speed of light) | PHP 7.2.16, WP 5.2
No Hooks.
Returns
true|false. True - the specified version of WordPress is compatible or the version for compatibility check is not specified ('', false, etc.). False - the specified version is not compatible.
Usage
is_wp_version_compatible( $required );
- $required(string) (required)
- The minimum required version of WordPress.
Examples
#1 Examples of test results
get_bloginfo( 'version' ); // 5.2 is_wp_version_compatible( '4.9' ); // true is_wp_version_compatible( '5.2' ); // true is_wp_version_compatible( '5.2.1' ) // false is_wp_version_compatible( '5.5' ); // false
#2 Display a message about an outdated version of WordPress
Suppose we wrote a plugin that will run only on WordPress 5.2 or higher. Then we must provide that the main code of the plugin does not run if the version of the installed WordPress is lower.
Suppose the user has WP 4.9, then:
<?php
if ( is_wp_version_compatible( '5.2' ) ) {
require_once __DIR__ . '/main-file-plugin.php';
}
else {
add_action( 'admin_notices', 'admin_php_version__error' );
}
function admin_php_version__error() {
?>
<div class="notice notice-error">
<p>The plugin requires WordPress version 5.2 or higher.
</div>
<?php
}
The code above is incomplete and only shows how the notification should work.
Notes
- Global. String.
$_wp_tests_wp_versionThe WordPress version string. Used only in Core tests.
Changelog
| Since 5.2.0 | Introduced. |