is_wp_version_compatible()WP 5.2.0

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().

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

0

#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
0

#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_version The WordPress version string. Used only in Core tests.

Changelog

Since 5.2.0 Introduced.

is_wp_version_compatible() code WP 7.0

function is_wp_version_compatible( $required ) {
	if (
		defined( 'WP_RUN_CORE_TESTS' )
		&& WP_RUN_CORE_TESTS
		&& isset( $GLOBALS['_wp_tests_wp_version'] )
	) {
		$wp_version = $GLOBALS['_wp_tests_wp_version'];
	} else {
		$wp_version = wp_get_wp_version();
	}

	// Strip off any -alpha, -RC, -beta, -src suffixes.
	list( $version ) = explode( '-', $wp_version );

	if ( is_string( $required ) ) {
		$trimmed = trim( $required );

		if ( substr_count( $trimmed, '.' ) > 1 && str_ends_with( $trimmed, '.0' ) ) {
			$required = substr( $trimmed, 0, -2 );
		}
	}

	return empty( $required ) || version_compare( $version, $required, '>=' );
}