Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My solution for cache busting in WordPress (drop-in to `/mu-plugins/` in Bedrock).
<?php
/**
* Replaces query version in registered scripts or styles with file modified time
*
* @link https://wordpress.stackexchange.com/a/346024/51130 based on
*
* @param $src
*
* @return string
*/
function add_modified_time( $src ) {
$clean_src = remove_query_arg( 'ver', $src );
$path = wp_parse_url( $src, PHP_URL_PATH );
$filename = untrailingslashit( ABSPATH ) . '/..' . $path;
if ( $modified_time = @filemtime( $filename ) ) {
$src = add_query_arg( 'ver', $modified_time, $clean_src );
} else {
// $src = add_query_arg( 'ver', time(), $clean_src );
}
return $src;
}
add_filter( 'style_loader_src', 'add_modified_time', 99999999, 1 );
add_filter( 'script_loader_src', 'add_modified_time', 99999999, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment