Snippet: Force WordPress CSS Changes Live
13 December 2016
Over the coming weeks I’m going to be posting some of the most useful snippets that I’ve found when building bespoke WordPress themes as a Freelance WordPress Developer.
I’m sure every front end developer has been in that situation whereby you’ve made an update to a site, and you go back to the client only for them to say “Oh, it still looks the same”. This is normally down to the fact the stylesheet has been cached, and the client needs to do a ‘hard refresh’ to get the latest version.
One way of fixing this is to simply change your CSS version number at the end of your CSS file, and this will ensure that the browser downloads the latest version. However, if you are doing multiple changes quickly then it can be bit of a faff!
So, here is a useful snippet that I use when enqueueing CSS files…
wp_enqueue_style( 'theme-styles', get_stylesheet_directory_uri() . '/style.css', array(), filemtime( get_stylesheet_directory() . '/style.css' ) );
This snippet basically changes the version number to the timestamp that the file was last modified. This ensures that your client is always seeing the most up to date CSS file!