If you’re writing a WordPress plugin, and you need to grab your own plugin version number (and don’t want to have to hard code it and remember to change it for each new update) then you can make use of the following:
/**
* Returns current plugin version.
*
* @return string Plugin version
*/
function plugin_get_version() {
if ( ! function_exists( 'get_plugins' ) )
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$plugin_folder = get_plugins( '/' . plugin_basename( dirname( __FILE__ ) ) );
$plugin_file = basename( ( __FILE__ ) );
return $plugin_folder[$plugin_file]['Version'];
}
If your plugin is not written as a class, then you’ll probably want to rename the function to yourpluginname_version() so that your don’t get a clash with any other functions of the same name.
After finding your code (thanks) I’ve stumbled upon simpler version fetch. Here’s more simple version:
/**
* Returns current plugin version.
*
* @return string Plugin version
*/
function plugin_get_version() {
$plugin_data = get_plugin_data( __FILE__ );
$plugin_version = $plugin_data['Version'];
return $plugin_version;
}
Cheers!
Definitely much simpler, and definitely the better way to go – thanks for sharing!
When I tried your example the function get_plugin_data was not found. However the initial written by Gary worked for me.