can’t see live version of style.css – is it web server caching?

If you update your WordPress theme’s style.css, you may have noticed that you have to “force-reload” your site in your browser to see the changes. This is because your browser keeps a copy of the CSS cached on your hard drive. Depending on how your server is set up, it may not check for a new version of the stylesheet for a couple hours, or longer! And even if you force-reload to see the changes, visitors who have previously accessed your site may still get the old CSS. One way to solve this is to “version” your CSS file, by adding ?v=123 to the URL in the to your stylesheet. This is rather a pain to have to do by hand every time, so here is a much better way:

<?php
$file = get_bloginfo( 'stylesheet_url' )
    . '?' . filemtime( get_stylesheet_directory() . '/style.css' );
?>
<link rel="stylesheet" href="https://wordpress.stackexchange.com/questions/129442/<?php echo $file; ?>" type="text/css" />

This automatically updates the ?12345678 ending part every time you modify the file. Now everyone instantly sees your changes.

Leave a Comment