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" />

See also:


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

5 Comments

  1. Chris Peacock webguru-webdesigns.com
    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!

  2. Ash Goodman thinkinginvain.com
    Feb 18th, 2010

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

  3. Chris Peacock webguru-webdesigns.com
    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!

    • Vladimir prelovac.com
      Feb 28th, 2010

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

  4. ipadku hpipad.com
    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

vladimir prelovac Hi! My name is Vladimir Prelovac. I am a computer engineer by profession and an adventurer by state of mind.

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

Books by Vladimir

WordPress Plugin Devleopment Book WordPress Plugin Development: Beginner's Guide

Published by Packt Publishing, available online through Amazon. Click the image for more information.

Consulting Services

Professional WordPress Services, including custom developed plugins and themes

Expert on-site WordPress SEO consulting and an 'out-of-the-box thinking' approach to problems