Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.