<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.
Will fail, apparently because
get_bloginfo('sylesheet_url')returns an absolute path and that upsets the filemtime() function.
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
And for your JS…
Agustin….on the contrary…it fails because it returns a path relative to document root…
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.
Thank you, sir! This is exactly what I have been looking for!
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.
only problem is it renders your site completely blank. uninstalled. thanks for the panic.
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
I released a plugin that just works doin’ the drink, it’s Style.css Load Last Version plugin for WordPress.
In 2017 your plugin saved my ass! Thank you!
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 Pastie.org
Thanks to Tiago Noronha, for WooThemes Support Team.
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…
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…
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:
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.
Here is a plugin https://wordpress.org/plugins/wp-css-version-history/ 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.