Created
January 2, 2020 05:53
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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