Skip to content

Instantly share code, notes, and snippets.

@danielbachhuber
Created September 26, 2013 19:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danielbachhuber/6719651 to your computer and use it in GitHub Desktop.
Save danielbachhuber/6719651 to your computer and use it in GitHub Desktop.
Automatically append mtime to script and style versions for cache-busting action
<?php
/** Automatically append mtime to script and style versions for cache-busting action **/
add_action( 'wp_enqueue_scripts', function() {
global $wp_styles, $wp_scripts;
foreach( array( 'wp_styles', 'wp_scripts' ) as $resource ) {
foreach( $$resource->registered as $name => $registered_resource ) {
// Not hosted here
if ( false === stripos( $registered_resource->src, home_url() ) )
continue;
$file = str_replace( 'wordpress/', '', str_replace( home_url( '/' ), ABSPATH, $registered_resource->src ) );
$mtime = filectime( $file );
$$resource->registered[$name]->ver = $$resource->registered[$name]->ver . '-' . $mtime;
}
}
}, 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment