Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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