Something to keep in mind when you are creating and maintaining WordPress Plugin code is to ensure you are also keeping its scripts and stylesheets versions up-to-date; and possibly more importantly, relevant to the current plugin version. If you are including your own JavaScript or custom stylesheet then you should also be providing a version number for them. I recommend using the same version number as the actual plugin so it is consistent and easily recognized. Now the question is: how do I do this easily and dynamically?

First off, you can gather up the plugin data with a relatively simple (and generic) function:

/**
 * Plugin Data
 * Returns the plugin header data as an array
 *
 * @uses       get_plugin_data
 *
 * @return array
 */
function wpfa_plugin_data() {
    /** Call the wp-admin plugin code */
    require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
    /** @var $plugin_data - holds the plugin header data */
    $plugin_data = get_plugin_data( __FILE__ );

    return $plugin_data;
} /** End function - plugin data */

There we go, now we have the plugin header data in a nice little array that we can work with … and this function is easily used as a method in your plugin class. You are putting your plugins into their own classes, right? (Yes, I know, the WPFA Sample Widget needs to be updated to move all of its code into the class.)

Now, let’s look at the standard enqueue functions you would use to add your scripts and/or styles to a plugin. This is for scripts …

<?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>

… as found in the WordPress codex here. We’ll be looking at the $ver parameter for our purposes.

The following is for stylesheets …

<?php wp_enqueue_style( $handle, $src, $deps, $ver, $media ); ?>

… as found in the WordPress codex here; and, again, we’ll be looking at the $ver parameter for our purposes here as well.

Let’s create a simple function/method to be used for enqueueing our scripts and styles.

/**
 * Plugin Scripts and Styles
 * Add plugin scripts and styles to be used on front facing views
 *
 * @uses       plugin_dir_url
 * @uses       wp_enqueue_script
 * @uses       wp_enqueue_style
 */
function Scripts_and_Styles() {

    /** @var object $header_data - plugin header data */
    $header_data = wpfa_plugin_data();

    /** Enqueue Scripts; 'jQuery' is enqueued as a dependency */
    wp_enqueue_script( 'WPFA-Sample-Script', plugin_dir_url( __FILE__ ) . 'wpfa-sample-script.js', array( 'jquery' ), $header_data['Version'], true );

    /** Enqueue Styles */
    wp_enqueue_style( 'WPFA-Sample-Style', plugin_dir_url( __FILE__ ) . 'wpfa-sample-style.css', array(), $header_data['Version'], 'screen' );

} /** End function - scripts and styles */

… and were done! Now your plugin script and stylesheet versions will always match the version of your plugin; and, you will never need to worry about a script or stylesheet using an older cached version because it has the same version number as the last one you released.

You should already be hooking the Scripts_and_Styles function but for completeness I will add that action call here as well:

/** Add Scripts and Styles */
add_action( 'wp_enqueue_scripts', 'Scripts_and_Styles' );

This is easily translated into class friendly code but I will leave that as an exercise for the reader.

Enjoy!