CSS Cache Buster for WordPress Themes

A little while ago, I expressed my joy at being able to avoid CSS cacheing-related issues on websites that could result in poor user experience, by using the CSS Cache Buster plugin.

Unfortunately, this has stopped working for me 🙁 Not sure if it’s the version of WordPress, or because I use child theming now. Either way, the demand for this fix maintained, so I’ve developed my own function for achieving this (leaning heavily on snippets from around the web, as always).


// Add date-modified query to stylesheet to prevent CSS caching when CSS has changed.
function wpi_stylesheet_uri($stylesheet_uri, $stylesheet_dir_uri){

if (!defined('STYLESHEETPATH'))
define('STYLESHEETPATH', get_stylesheet_directory());

$csslastmodified = filemtime(STYLESHEETPATH.'/style.css');

if (!$csslastmodified == '') {
return $stylesheet_dir_uri.'/style.css?' . $csslastmodified;
} else {
return $stylesheet_dir_uri.'/style.css';
}
}
add_filter('stylesheet_uri','wpi_stylesheet_uri',10,2);

As always, very happy to to be corrected, or for this to be improved upon, but it works great for me 🙂

One comment

Comments are closed.