wp_get_wp_version()
Gets the current WordPress version.
Safely obtains the current WordPress version number by accessing the global variable $wp_version directly: retrieves data from the file wp-includes/version.php directly.
Some plugins modify the global variable $wp_version to hide the site version from attackers, but this practice can break other parts of WordPress or plugins that rely on the correct version.
No Hooks.
Returns
String. String with the current WordPress version.
Usage
wp_get_wp_version();
Examples
#1 Demo
echo wp_get_wp_version(); // 6.8.3
#2 Conditional loading of styles depending on WP version
Execute code only for specific WordPress versions. In the example below we will enqueue different stylesheet files for WordPress versions before and after 6.7. This is useful for ensuring backward compatibility.
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
// current version is less than 6.7
if ( version_compare( wp_get_wp_version(), '6.7', '<' ) ) {
wp_enqueue_style( 'my-theme-legacy', get_template_directory_uri() . '/css/legacy.css' );
}
else {
wp_enqueue_style( 'my-theme-modern', get_template_directory_uri() . '/css/modern.css' );
}
}
Changelog
| Since 6.7.0 | Introduced. |
wp_get_wp_version() wp get wp version code WP 6.9.1
function wp_get_wp_version() {
static $wp_version;
if ( ! isset( $wp_version ) ) {
require ABSPATH . WPINC . '/version.php';
}
return $wp_version;
}