Last active
August 1, 2018 15:06
-
-
Save salcode/b627efd3122155e657688914dfaa298f to your computer and use it in GitHub Desktop.
Force WordPress to load the most recent version of the child theme's style.css file.
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 | |
/** | |
* Filter all CSS URLs being loaded on the page. | |
* Add the last modified time (in Unix Time, i.e. a large integer) | |
* to the end of the URL. | |
* | |
* This forces the browser to load the most recent version of this file. | |
* e.g. http://example.com/wp-content/my-child-theme/style.css | |
* becomes | |
* http://example.com/wp-content/my-child-theme/style.css?ver=1533080875 | |
* | |
* Quick Setup: | |
* 1. Navigate (or create) the mu-plugins directory | |
* 2. From the command line run | |
* curl -O https://gist.githubusercontent.com/salcode/b627efd3122155e657688914dfaa298f/raw/add-file-mod-time-as-ver-number-to-css.php | |
*/ | |
add_filter( 'style_loader_src', function( $src, $handle ) { | |
// All of my CSS changes are made in a child-theme. | |
if ( 'child-theme' !== $handle ) { | |
// This is not the child-theme CSS file, make no changes. | |
return $src; | |
} | |
// Use the default location for the child theme CSS file. | |
$file_path = get_stylesheet_directory() . '/style.css'; | |
if ( ! is_file( $file_path ) ) { | |
// No file exists in the default child theme CSS file location. | |
// Make no changes. | |
return $src; | |
} | |
$last_modified = filemtime( $file_path ); | |
$src = add_query_arg( 'ver', intval( $last_modified ), $src ); | |
return $src; | |
}, 9999, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment