WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#10529 closed enhancement (wontfix)

API to retrieve current WordPress version

Reported by: stephanreiter Owned by:
Milestone: Priority: normal
Severity: minor Version:
Component: Plugins Keywords: close version query
Focuses: Cc:

Description

Sometimes it's necessary in plug-ins to check whether a certain functionality or behavior is supported by the current WP version or not. For this purpose I currently use the following code in the Scissors plugin:

function _minimalWpVersion($major, $minor) {
  global $wp_version;
  $version = preg_replace("/[^\\.0-9\s]/", "", $wp_version); // keep only '0' .. '9' and '.'
  $version = explode('.', $version);
  return (count($version) >= 2 && ($version[0] > $major || $version[0] == $major && $version[1] >= $minor));
}

I would love to see the introduction of an API that allows plug-in authors to easily query the current version of WordPress and get a result in the form of an array('major' => 2, 'minor' => 8, 'revision' => 2) (to give an example). This would eliminate potential problems when the format of the version-string changed.

Change History (4)

comment:1 @dd326 years ago

php's version_compare() works well:

global $wp_version;
if ( version_compare($wp_version, '2.8', '>=') ) {
//2.8+ stuff here
}

the good thing about this, is that it understands major, minor and point release, as well as dealing with -dev -alpha -etc.. nativly.

comment:2 @Denis-de-Bernardy6 years ago

  • Keywords close added

seems like unneeded bloat. use version_compare().

comment:3 @stephanreiter6 years ago

  • Resolution set to wontfix
  • Status changed from new to closed

Thanks for the hint. I didn't know about version_compare, which seems to do exactly what I need!

comment:4 @Denis-de-Bernardy6 years ago

  • Milestone 2.9 deleted
Note: See TracTickets for help on using tickets.