Cache Busting WordPress Style.css

Published 14th November 2013

Say you have W3 Total Cache running on your WordPress site and you have your CSS set up to be served from a CDN, like MaxCDN for example. Any time you upload changes to your style.css file you have to purge the CDN or (if your a bit smarter) have to at least update the verison query string of the style.css file.

Assuming that you are already including your style.css file using wp_enqueue_scripts and not embedding directly it in header.php you can do the following to automatically update the version query string of style.css, making the CDN serve the new version of the file without touching anything.

wp_register_style( 'screen', get_template_directory_uri().'/style.css', array(), filemtime( get_template_directory().'/style.css' ) ); wp_enqueue_style( 'screen' ); // Example Output: /style.css?ver=1384432580

What we are doing here is using filemtime() to pass in the last modified timestamp of style.css as the version of the file. Thus changing the query string any time the file is updated, automatically purging the CDN for you. Easy!