Code Snippet

Home » Code Snippets » WordPress » Prevent CSS Caching

Prevent CSS Caching

WordPress:

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

bbPress:

<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.

Subscribe to The Thread

  1. 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. 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. 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. Christian Sciberras

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

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~