How to Get The WordPress Plugin Version From a Plugin Header Block

If you’re developing a new plugin, you may find it useful to get the plugin version of your plugin from the Plugin Header block for use in other areas of your plugin such as query ? strings used in included files like script.js and style.css files. This technique will explain how to easily grab the “Version” value from your plugin header to use throughout your plugin.

Example Plugin Header

Below is a basic example of a WordPress Plugin Header:

/**
 * Plugin Name:       My Basics Plugin
 * Plugin URI:        https://example.com/plugins/the-basics/
 * Description:       Handle the basics with this plugin.
 * Version:           1.10.3
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            John Smith
 * Author URI:        https://author.example.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       my-basics-plugin
 * Domain Path:       /languages
 */

Getting the Plugin Version Value

The value we would like to use in our plugin is “1.10.3” which is the “Version” number and we can easily get that value by adding the following php code to the file that contains this Plugin Header block:

$plugin_data = get_file_data( (__FILE__), array('Version' => 'Version'));

$plugin_version = $plugin_data['Version'];

Optionally, you could just use the following one liner instead:

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

Using the Plugin Version Throughout The Plugin

Now that we have the value of the plugin version in the $plugin_version variable, there are a few ways we can use it. The first thing I usually do is define a php constant for my plugin version. You can use any constant as long as it’s unique. I usually prepend the constant name with several letters of my plugin name or purpose and then “_PLUGIN_VERSION” like below.

Since the plugin name is “My Basic Plugin”, I’m going to define a PHP constant such as MYBP_PLUGIN_VERSION. Below you will see the full line of PHP to do so.

define( 'MYBP_PLUGIN_VERSION', $plugin_version );

Now that we have the constant defined, we can simply call MYBP_PLUGIN_VERSION anywhere in our plugin and it will always get the current plugin version I have defined in my plugin header.

The perfect use case for this is in cache busting for .js and .css files.

Here’s an example of how you could use it for a javascript file you have registered:

wp_register_script(
     'mybp_plugin_js',
     MY_PLUGIN_URL . 'js/public.js',
     array('jquery'),
     MYBP_PLUGIN_VERSION,
     false
);

Now, if this javascript file is used on the front of your site, when you view the source code of a page where this is being included, you will see a version number appended to the public.js file like this: js/public.js?ver=1.10.3

Whenever you update your plugin’s public.js file, just increment your plugin version in your plugin header and the web browser will automatically clear the cache for that file. This is also known as “Cache Busting” and is widely used by plugin Developers to ensure updates don’t break a plugin’s functionality.

Have any questions? Leave a comment below!

Leave a Comment