Displaying Your WordPress Plugin Version in the Plugin Row: A Quick Guide
Are you developing a WordPress plugin and want to showcase its current version directly within the plugin row on the WordPress dashboard? This is a common practice, providing users with transparency and easily indicating updates. This guide will walk you through the process using the get_plugin_version
function and the plugin_row_meta
filter.
Understanding the Basics
get_plugin_version
Function: This built-in WordPress function retrieves the version number of a specific plugin.plugin_row_meta
Filter: This filter lets you add custom metadata to the plugin row, providing you with the perfect space to display the version.
Steps to Display the Version
-
Retrieve the Plugin's Version:
- Within your plugin's main PHP file, use the
get_plugin_version
function to obtain the version number. - This function takes the plugin's file path as an argument:
$plugin_version = get_plugin_version( __FILE__ );
- Within your plugin's main PHP file, use the
-
Hook into the
plugin_row_meta
Filter:- Use the
add_filter
function to hook your custom code into theplugin_row_meta
filter. - Within the filter function, access the plugin's meta data (passed as an argument), add the version information, and return the updated meta data.
add_filter( 'plugin_row_meta', 'my_plugin_version', 10, 2 ); function my_plugin_version( $plugin_meta, $plugin_file ) { if ( $plugin_file == __FILE__ ) { $plugin_meta[] = 'Version: ' . $plugin_version; } return $plugin_meta; }
- Use the
-
Activate and Verify:
- Activate your plugin.
- Navigate to the 'Plugins' section in your WordPress dashboard.
- You should now see the version number displayed within the plugin's row.
Example Scenario
Let's assume you have a plugin named 'My Amazing Plugin' stored in the file my-amazing-plugin.php
. You want to display the version information in the plugin row. Here's how you'd implement it:
Important Notes:
- Version Number in the Header: Ensure your plugin's header comment includes a correct
Version
line. This provides the initial version information used byget_plugin_version
. - Plugin File Path: Double-check that you're using the correct file path when calling
get_plugin_version
and comparing it in the filter function. - Filter Priority: The
plugin_row_meta
filter uses priority values. Setting it to10
(as in the example) ensures your version is displayed in the default location within the plugin row.
Conclusion
By using the get_plugin_version
function and the plugin_row_meta
filter, you can easily and effectively display your WordPress plugin's version number directly within the plugin row on your WordPress dashboard. This provides valuable transparency for your users and makes it easy to identify the latest version of your plugin.