Cache busting CSS files other than style.css

All right, so we’re probably all familiar with the typical way to ensure that your main CSS file is refreshed when you load the page (busting the browser cache), right?

<link rel="stylesheet" type="text/css" media="all"
  href="https://wordpress.stackexchange.com/questions/5116/<?php echo get_stylesheet_uri();
    echo"?'.filemtime( get_stylesheet_directory() . '/style.css'); ?>" />

I need to do the same thing on another CSS file. Yes, my style.css file has its own set of @import “css/myFile.css” which are working fine, but humor me, if you will.
So, back to header.php, right after that first link, and before the call to wp_head():

<?php $cssFile = get_stylesheet_directory_uri().'/css/other.css'; ?>
<link rel="stylesheet" type="text/css" media="all"
  href="https://wordpress.stackexchange.com/questions/5116/<?php echo $cssFile; echo"?'.filemtime($cssFile); ?>" />

And this leads to a warning (as part of the href attribute of the link when the browser gets the page):

Warning: filemtime(): stat failed for http://localhost/wordpress/wp-content/themes/my_theme/css/other.css

the path to the file seems to be built correctly (and the other.css file is there), but filemtime (stat, actually) fails on it. How come?

Other recommended ways to include the ‘latest’ version of a CSS file other than style.css?
Should I go with wp_register_style instead? If so… how can I tell wp_register_style to bust the browser cache (ie: get me the latest version of the css file, even if the browser has it cached)?
Thanks in advance

6 s
6

You’re using the file’s path in the first call, but its URL in the second. So it won’t work.

Leave a Comment