How to prevent CSS caching and load the most recent version of a file after updating

When working with clients you need to make sure any CSS changes are loading and website is updated. Every time a browser loads a webpage it has to download a number of files, including CSS and JavaScript. A common solution is to use CSS versioning. Adding a version number to the CSS file will let the server know this is a new file. You can automatically version your CSS files by adding a last modified timestamp to the end of the file name like /css/style.css?ver=1469128847

A solution in WordPress:

Version number is a parameter of wp_enqueue_style().

Codex:

We’ll use the filemtime() function to return the last time the file content was modified.

Code:

Result:

Join the Conversation

2 Comments

Leave a comment

Your email address will not be published. Required fields are marked *