close
close
Wordpress Get Plugin Version And Display Via Plugin Row Meta

Wordpress Get Plugin Version And Display Via Plugin Row Meta

2 min read 01-01-2025
Wordpress Get Plugin Version And Display Via Plugin Row Meta

Knowing your plugin versions is crucial for updates, troubleshooting, and maintaining a secure WordPress site. This post will guide you through retrieving and displaying your plugin's version number directly within the plugin row meta in your WordPress admin dashboard. This provides a clean and convenient way to identify your plugin's version without navigating to the plugin's settings page.

Understanding Plugin Row Meta

The plugin row meta is the information displayed under the plugin name in your WordPress plugins page. By default, this includes actions like 'Deactivate' and 'Delete', but you can add custom information—such as the version number—using a filter.

Retrieving the Plugin Version

To display the version, you first need to access it. This is typically done by reading the plugin's header information. Your plugin's main file (usually my-plugin.php) contains a header comment block including a Version line. For example:

<?php
/**
 * Plugin Name: My Awesome Plugin
 * Plugin URI:  https://example.com/my-awesome-plugin
 * Description: This is a fantastic plugin!
 * Version:     1.2.3
 * Author:      Your Name
 * Author URI:  https://yourwebsite.com
 * License:     GPL2
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: my-awesome-plugin
 */

// Rest of your plugin code...

The Version line (here: 1.2.3) is the key to getting the version information programmatically.

Displaying the Version in Row Meta

We'll use the plugin_row_meta filter to add our version information to the plugin row. Create a file (e.g., my-plugin-meta.php) and add the following code:

<?php

/**
 * Add plugin version to the plugin row meta.
 *
 * @param array  $plugin_meta An array of the plugin's metadata.
 * @param string $plugin_file Path to the plugin file.
 * @return array Filtered plugin metadata.
 */
function add_my_plugin_version( $plugin_meta, $plugin_file ) {

    if ( $plugin_file == 'my-plugin/my-plugin.php' ) { // Replace 'my-plugin/my-plugin.php' with your plugin's path

        $plugin_data = get_plugin_data( $plugin_file );
        $version = $plugin_data['Version'];

        $plugin_meta[] = sprintf( __( 'Version: %s', 'my-awesome-plugin' ), $version ); // Replace 'my-awesome-plugin' with your plugin's text domain
    }

    return $plugin_meta;
}

add_filter( 'plugin_row_meta', 'add_my_plugin_version', 10, 2 );

Remember to:

  • Replace 'my-plugin/my-plugin.php' with the correct path to your plugin's main file.
  • Replace 'my-awesome-plugin' with your plugin's text domain (defined in the plugin header).
  • Place this code in a file within your plugin directory and ensure it's included in your main plugin file.

This code retrieves the plugin version using get_plugin_data and appends it to the $plugin_meta array. The plugin_row_meta filter then ensures this updated information is displayed on the plugin's row in the admin. This provides a simple, yet effective way to add version details for better plugin management. Always thoroughly test your code after implementation.

Related Posts


Popular Posts