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. 😛
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?
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?
There is a function called get_plugin_data(). Try calling this from within the main plugin file if you need to:
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.
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:
Under the hood
get_file_data
does some cleaver scanning to be quite performant.And if needed define your constant:
One possible solution can be regex:
Must mention that this regex is a bit faster than
get_file_data()
but in the general you will not notice it.Just like
wp_get_theme()
function, there isget_plugin_data()
function. That returns plugin data e.gname, version, description, author, etc...
Before calling the function, make sure it’s available, otherwise, you will get the error ‘call to undefined function’.
You can define a constant in your main plugin’s php file.
Then if you want to output the version in subsequent pages within your plugin folder, just use;
Don’t forget to Replace ‘MY_PLUGIN’ With the actual name of your plugin