Prevent CSS Caching


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


<link rel="stylesheet" href="<?php bb_stylesheet_uri(); echo '?' . filemtime( bb_get_active_theme_directory() . '/style.css'); ?>" type="text/css" media="screen" />

Adds stylesheet with time of last update. If the number changes the browser updates the CSS instead of using cached version. Easily amended to be none WP specific.


  1. User Avatar
    Agustín Amenabar L. (@baamenabar)
    Permalink to comment#

    In WordPress,

    filemtime(get_bloginfo( 'stylesheet_url' ));

    Will fail, apparently because get_bloginfo('sylesheet_url') returns an absolute path and that upsets the filemtime() function.

  2. User Avatar
    Permalink to comment#

    I’m trying to move a website to a different server, downloaded all the files the new company put them up for me but I have lost all my css formatting for my theme (striking). Can find where the original developer of the website make the changes to header sizes, link colours etc etc. Could you help me maybe. Please

  3. User Avatar
    Permalink to comment#

    And for your JS…

    <script type="text/javascript" src="<?php echo get_bloginfo('template_directory') . '/js/script.js?' . filemtime( get_stylesheet_directory() . '/js/script.js');?>"></script>
  4. User Avatar
    Christian Sciberras
    Permalink to comment#

    Agustin….on the contrary…it fails because it returns a path relative to document root…

  5. User Avatar
    Martin Klasson
    Permalink to comment#

    I am getting a “Stat”-php error for the example given above.
    The solution that I worked out that was working for me was this:

    <link rel="stylesheet" href="/css/style.css?version=”>

    What I have done is used the dirname(_FILE_) to get the path to the directory. Not the URL, but the PATH on the computer.

    The code above is used for my custom css-class. (I dont use the default css), but can easily be amended of course to work along with the stylesheet_url as well.

  6. User Avatar
    Permalink to comment#

    Thank you, sir! This is exactly what I have been looking for!

  7. User Avatar
    Permalink to comment#

    There is a plugin up on the WordPress Plugin Repository that takes care of this automatically. Fixes version numbers for stylesheets and scripts. WordPress plugin Versions. Works really good.

    • User Avatar

      only problem is it renders your site completely blank. uninstalled. thanks for the panic.

  8. User Avatar
    Permalink to comment#

    This is exact copy paste from Mark’s site, without any reference. You have only changed the subject and added the bbpress stuff…. rest everything is same

  9. User Avatar
    Permalink to comment#

    I released a plugin that just works doin’ the drink, it’s Style.css Load Last Version plugin for WordPress.


  10. User Avatar
    Permalink to comment#

    Hello guys!

    For those who uses WooThemes Canvas, child theme, and want to automatically add version to custom.css file, here is the code (put it in your functions.php):

    View in

    Thanks to Tiago Noronha, for WooThemes Support Team.


  11. User Avatar

    I know this is old, but for WordPress, in your functions.php file, why not just pass NULL to the version parameter of wp_enqueue_style?…It will do the same thing without having to hard code the link into the header, if no dependencies use null as well…

    wp_enqueue_style('your-theme', get_template_directory_uri() . '/style.css', NULL, NULL, 'screen');
    • User Avatar

      of course that is, if you would like to prevent in dev stage…when you push live remove the nulls and the version num will pop back in again…

    • User Avatar
      Permalink to comment#

      For me it didn’t do the job. And I can’t see why it should. If you pass NULL to the version parameter then the version never changes and therefore you don’t force your browser to reload it.

      Please correct me if I’m wrong. I’d really like to understand how this works better.

      Anyway, what I used in WordPress was this code:

      add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
      function enqueue_child_theme_styles() {
          wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
          wp_enqueue_style( 'child-style', get_stylesheet_uri(), NULL, filemtime( get_stylesheet_directory() . '/style.css' ) );

      I prefer this over the orginal code-snippet from css-tricks as it is the best way to include a childtheme-stylesheet in WordPress combined with filemtime() to force browsers to load new versions of the css-files. I didn’t use filemtime() for the parent-style as I never touch it and therefore filemtime() would only waste resources.


  12. User Avatar
    Brian Holzberger

    Here is a plugin that will automatically append a version number in the stylesheet. It creates a new stylesheet which is loaded last. No need to clear cache to see changes. Uses WordPress built in CSS editor and user file lock for team collaboration.

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.