is_wp_version_compatible()WP 5.2.0

Checks compatibility with the current WordPress version.

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.

Return

true|false. True if required version is compatible or empty, false if not.

Usage

is_wp_version_compatible( $required );
$required(string) (required)
Minimum required WordPress version.

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_version The WordPress version string.

Changelog

Since 5.2.0 Introduced.

is_wp_version_compatible() code WP 6.5.2

function is_wp_version_compatible( $required ) {
	global $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, '>=' );
}