function pie_enqueue_style($handle = 'None',$src = false,$deps = array(),$ver = false, $media = false ){
$ver = $ver === false ? pie_cb_version($src) : $ver;
wp_enqueue_style($handle,$src,$deps,$ver,$media);
}
function pie_enqueue_script($handle = 'None',$src = false, $deps = array(),$ver = false, $in_footer = false){
$ver = $ver === false ? pie_cb_version($src) : $ver;
wp_enqueue_script($handle,$src,$deps,$ver,$in_footer);
}
function pie_cb_version($src){
$src = parse_url($src) ? $src : $_SERVER["DOCUMENT_ROOT"].$src;
return file_exists($file) ? filemtime($file) : 1;
}
These are three functions to overwrite the default version parameter that Wordpress appends to script & style URLs when using wp_enqueue_script() and wp_enqueue_style(). The WP version parameter is replaced with Cachebusting parameters based on the last time a local file was modified.
For example...
`<script type='text/javascript' src='/wp-content/themes/mytheme/js/modernizr-1.5.min.js?ver=3.1.1'></script> `
...becomes...
` <script type='text/javascript' src='/wp-content/themes/mytheme/js/modernizr-1.5.min.js?ver=1300881484'></script> `
The effect is that if a file has been changed since it was last cached by a users browser, then the browser will be forced to reload the file, otherwise it will use the cached version. At the moment, it is only set up to work with absolute URLs or URLs relative to the site root (starting with a '/'). Worth noting is that if a file is not found, the function return '1' as the version parameter, thus keeping the WP version hidden.
I'm very interested in people's thoughts on these functions and possible ways to improve them, or if there's a better way of going about achieving this altogether.
EDIT: I've changed the pie_cb_version() to detect any valid URL versus a relative path (formerly it only looked for strings starting with 'http://') - credit for spotting this flaw to @mattwiebe