Is there a way for a plug-in to get it’s own version number?

Is there an API I can call, inside my plug-in, to determine the plug-in’s version?

I just want to have my plug-in emit a html comment with it’s own version number … for diagnostic purposes.

Related posts

Leave a Reply

3 comments

  1. @david: Both @Adam Backtrom and @Viper007Bond give some good advice so I thought I’d take their advice and see if I couldn’t implement something, see below.

    What follows iS a plugin called WP Active Plugins Data that parses the header metadata for all active plugins anytime any plugin is activated and it stores all the metadata for each plugin into an array option in wp_options. I designed it for both regular WordPress plugins and multisite site-wide plugins. You can download it here from gist but I’ve also copied the code here for your review:

    <?php
    /*
    Plugin Name: WP Active Plugins Data
    Plugin URI: http://mikeschinkel.com/wordpress-plugins/wp-active-plugins-data/
    Description: Loads Plugin Data on Plugin Activation and Persists to wp_options for quick retrieval.
    Version: 0.1
    Author: Mike Schinkel
    Author URI: http://mikeschinkel.com
    Note: Written for http://wordpress.stackexchange.com/questions/361/is-there-a-way-for-a-plug-in-to-get-its-own-version-number
    */
    
    require_once(ABSPATH.'wp-admin/includes/plugin.php');
    
    function get_active_plugin_version($plugin_path_file, $sitewide = false) {
        return get_active_plugin_attribute($plugin_path_file,'Version');
    }
    function get_active_plugin_attribute($plugin_path_file, $attribute) {
        $all_plugins_data = get_active_plugins_data($plugin_path_file,$sitewide);
        return (isset($all_plugins_data[$attribute]) ? $all_plugins_data[$attribute] : false);
    }
    function get_active_plugins_data($plugin_path_file, $sitewide = false) {
        $failsafe = false;
        $plugin = plugin_basename(trim($plugin_path_file));
        $sitewide = (is_multisite() && ( $sitewide || is_network_only_plugin($plugin)));
        if ($sitewide) {
            $all_plugins_data = get_site_option('active_sitewide_plugin_data',array());
        } else {
            $all_plugins_data = get_option('active_plugin_data',array());
        }
        if (!$failsafe && !is_array($all_plugins_data) || count($all_plugins_data)==0) {
            $failsafe = true; // Don't risk infinite recursion
            if ($sitewide) {
                $active_plugins = get_site_option('active_sitewide_plugins',array());
            } else {
                $active_plugins = get_option('active_plugins',array());
            }
            persist_active_plugin_data(null,$active_plugins,$sitewide);
            $all_plugins_data = get_active_plugin_version($plugin_path_file,$sitewide);
        }
        return $all_plugins_data[$plugin_path_file];
    }
    add_action('update_site_option_active_sitewide_plugins','persist_sitewide_active_plugin_data',10,2);
    function persist_sitewide_active_plugin_data($option, $plugins) {
        persist_active_plugin_data(null,$plugins,'sitewide');
    }
    add_filter('update_option_active_plugins','persist_active_plugin_data',10,2);
    function persist_active_plugin_data($old_plugins, $new_plugins, $sitewide=false) {
        $active_plugin_data = array_flip($new_plugins);
        $plugin_dir = WP_PLUGIN_DIR;
        foreach($new_plugins as $plugin) {
            $active_plugin_data[$plugin] = get_plugin_data("$plugin_dir/$plugin");
        }
        if ($sitewide)
            update_site_option('active_sitewide_plugin_data',$active_plugin_data);
        else
            update_site_option('active_plugin_data',$active_plugin_data);
    }
    

    Want to see how it works? Here’s a test file you can drop in the root of your WordPress site (http://example.com/test.php.) Make sure you have both this plugin and Akismet activated before you test it.

    <?php
    /*
    * test.php - Place in root of WordPress website.
    *
    * Before running be sure to activate both Akismet and the WP Active Plugin Data plugin
    *
    */
    
    include "wp-load.php";
    
    header('Content-type:text/plain');
    $akismet = "akismet/akismet.php";
    echo "Akismet Version: " . get_active_plugin_version($akismet);
    echo "nnAkismet Description: " . get_active_plugin_attribute($akismet,'Description');
    echo "nnAll Akismet Data:n";
    print_r(get_active_plugins_data($akismet));
    

    If it’s not exactly what you need at least it should give you a good starting point. Hope this helps.

  2. There is in the admin screens: get_plugin_data(). In templates, I think you will need the plugin to hold that data in PHP, e.g., set a constant or global or something, and keep that value synchronized with the plugin header version number.

    wp-settings.php calls wp_get_active_and_valid_plugins(), which pulls data from the active_plugins site option. This option only contains the path to the plugin file, and wp-settings.php only runs include_once on the file, so it’s never parsed for the plugin metadata.

  3. You can parse your plugin’s meta data (that stuff at the top of the file), but it’s better for performance if you just set your own PHP variable with a matching version number. When you update the plugin, just update both version numbers.

    It’s slightly more work for you in the short term, but a lot better in the long term.