15

Howdy, I recently cribbed W3TC to implement an "in-update" changelist display (very cool), in my plugin, but there's an awkward bit of code I'd prefer to avoid.

If you look at the top of this file, you'll see the following code:

define ( 'BMLT_CURRENT_VERSION', '2.1.16' ); // This needs to be kept in synch with the version above.

Ick. :P

That needs to be kept up to date, so the function can delta between your plugin, and the current stable version.

I have perused the Codex, and can't find it, but there has GOT to be an API function for getting the version of a plugin.

Any clues?

6 Answers 6

3

Here is an answer with some code that will do what you want it to do: Is there a way for a plug-in to get it's own version number?

0
20

There is a function called get_plugin_data(). Try calling this from within the main plugin file if you need to:

$plugin_data = get_plugin_data( __FILE__ );
$plugin_version = $plugin_data['Version'];

But as is said in the answers to the other question, its better for performance to just define a PHP variable as you're doing.

5
  • Thanks! Hopefully, performance won't be a big deal, as this is only called once, and involves a curl call. I have just gotten into trouble from having multiple copies of the version number all over the place. May 25, 2011 at 20:36
  • 2
    it doesn't seem to work. I get this warning Call to undefined function get_plugin_data().
    – Ari
    Dec 5, 2013 at 7:53
  • @AriSusanto Are you trying to use it on the front end? The WP plugin administration functions are only defined in the admin section. If you need to use this on the front end you'll have to include the file wp-admin/includes/plugin.php. Dec 6, 2013 at 23:01
  • No, I use it on administration panel.
    – Ari
    Dec 7, 2013 at 2:56
  • If you have the undefined error, you can use get_plugin_data function. For the perfomance, I added a comment here: wordpress.stackexchange.com/questions/361/…
    – baptx
    Jan 11, 2018 at 21:06
16

An alternative to get_plugin_data() is get_file_data() which is available without the overhead of loading additional files.

Simply add this to your main plugin file:

$plugin_data = get_file_data(__FILE__, array('Version' => 'Version'), false);
$plugin_version = $plugin_data['Version'];

Under the hood get_file_data does some cleaver scanning to be quite performant.

And if needed define your constant:

define ( 'YOURPLUGIN_CURRENT_VERSION', $plugin_version );
2
  • I guess for performance it is also better to write the version number manually? (like explained here: wordpress.stackexchange.com/questions/361/…)
    – baptx
    Jul 18, 2019 at 14:00
  • @baptx yes, however, this is one optimization would be negligible considering everything else Wordpress does on every request. In other words, it doesn't matter
    – paulcol.
    Aug 13, 2019 at 22:19
1

One possible solution can be regex:

$plugin_version = NULL;
if(preg_match('/\*[\s\t]+?version:[\s\t]+?([0-9.]+)/i',file_get_contents( __FILE__ ), $v))
    $plugin_version = $v[1];

Must mention that this regex is a bit faster than get_file_data() but in the general you will not notice it.

1

Just like wp_get_theme() function, there is get_plugin_data() function. That returns plugin data e.g name, version, description, author, etc...

Before calling the function, make sure it's available, otherwise, you will get the error 'call to undefined function'.

if( ! function_exists('get_plugin_data') ){
    require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
$plugin_data = get_plugin_data( __FILE__, false );

define( 'BMLT_NAME', ($plugin_data && $plugin_data['Name']) ? $plugin_data['Name'] : 'Plugin Name' );
define( 'BMLT_CURRENT_VERSION', ($plugin_data && $plugin_data['Version']) ? $plugin_data['Version'] : '1.0.0' );
0

You can define a constant in your main plugin's php file.

define ('MY_PLUGIN_VERSION', '1.0');

Then if you want to output the version in subsequent pages within your plugin folder, just use;

<?php echo('MY_PLUGIN_VERSION'); ?>

Don't forget to Replace 'MY_PLUGIN' With the actual name of your plugin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.