Skip to content

Instantly share code, notes, and snippets.

@salcode
Last active August 1, 2018 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salcode/b627efd3122155e657688914dfaa298f to your computer and use it in GitHub Desktop.
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.
<?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