Skip to content

Instantly share code, notes, and snippets.

@marsjaninzmarsa
Created January 2, 2020 05:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marsjaninzmarsa/038ce27f1e76b90edd5773532e48520b to your computer and use it in GitHub Desktop.
Save marsjaninzmarsa/038ce27f1e76b90edd5773532e48520b to your computer and use it in GitHub Desktop.
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