Adding version to theme CSS file

This is a small trick I've been using on a number of sites where far expiration dates have been set on the server to improve browsing performance.

Basically if you often do any changes to your CSS (like me) the old version of the file will normally be served to your visitors as the cache expiration header is set to a far future time.

To remedy this we need to add a parameter to CSS file URL that will make it distinct every time you change it. Best way to do it is by using the timestamp of the file.

You can change the CSS reference in your header.php to something like this:

<link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_url'). '?' . filemtime( get_stylesheet_directory() . '/style.css'); ?>"  />

For example here is how my CSS URL looks like

<link rel="stylesheet" type="text/css" href="http://static.prelovac.com/style.css?1266235697" />

Related Articles:


Posted in: WordPress
TAGS:, , , , , , , , , , , , , , ,
leave a comment.

Comments:

8 Comments

  1. Ben Landen
    Oct 2nd, 2011

    this tip help me to do my work easy..thank you..keep posting

  2. iCosmin
    Jan 8th, 2011

    I've had issues in Firefox on a client site I was building.

    Looks like Firefox 3.x has some caching problems and that resulted in it not always being able to load my style.css.

    So I searched and found your solution, but as you say, it only works when the CSS changes.
    My CSS is not changing but stupid Firefox still can't load it after a few browsed pages.

    My solution, inspired by yours, was to use PHP's uniqid() function in order to append a unique number after the '?' at the end of the file URI.

    Here's how (please edit in case WordPress screws up the code)

    
    <link rel="stylesheet" type="text/css" href=""  />
    
    Hope this helps others that have the same issue.
    
    I know it's not very elegant, since it always reloads your CSS, but if you have it minified for the live site, it's not a big deal (ok, true, it depends on how much traffic you get, but still...)
    
    Thanks, Vladimir!
    
  3. Chris Peacock
    Mar 1st, 2010

    My mistake, I thought it just used today's date, but I understand it uses the files modified date (is that right, sorry I'm not familiar with filemtime function, guess I should google it). Definitely makes more sense to me now. Great idea!

  4. Ash Goodman
    Feb 18th, 2010

    Excellent! I am implementing it on all of my client websites going forward. Thank you for another excellent tip Vladimir.

  5. Chris Peacock
    Feb 17th, 2010

    Awesome trick! Nice way to keep from Ctrl-refreshing all the time. But I don't think it is a good idea to keep it that way. It is kind of like a debugging feature, you only want to use it when your doing lots of trial and error testing. Otherwise, every visit requires visitors to re-download the stylesheet, which increase the page load time a little bit. What about graphics? Should we do the same when we are fine tuning graphic designs, forcing visitors to re-download graphics on every page load? I know it's absurd, but proves my point I think. But still, awesome trick!

    • Feb 28th, 2010

      Every visit will not require a re-download, only when the CSS is changed.

  6. ipadku
    Feb 17th, 2010

    awesome trick master..

    its so can include author blog :)

Have your say

Your email is never published nor shared. Required fields are marked *

*
*

Subscribe without commenting

About

vladimir prelovac Vladimir Prelovac is CEO of Prelovac Media, a computer engineer by profession and an adventurer by state of mind. more +


"I would love to change the world, I just don't have the source code yet."

Services

Manage multiple WordPress sites

Built for WordPress enthusiasts, ManageWP helps you manage all your WordPress sites from one central location.

Books

WordPress Plugin Devleopment Book Read my book WordPress Plugin Development: Beginner's Guide

Published by Packt Publishing, available online through Amazon.