Removing the version stamp on WordPress CSS stylesheet

Posted in Uncategorized

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

If you look at the source code output of WordPress site, you see a version stamp on the css file.  For example, the reference to the main style.css stylesheet shows “style.css?ver=3.8.1″…

version stamp on stylesheet

version stamp on stylesheet

There might be times when you want to remove this version stamp.  Why?  First of all, sometimes the browser caches the style.css file even though it has changed.  You might want to replace the version stamp with a timestamp instead that act like a “cache buster”.  And second, it reviews the WordPress version (in our case it shows WordPress version 3.8.1), which is not a great thing in terms of security especially if you are on an outdated WordPress version (which should should not be).

The way to remove this version stamp is to edit the functions.php file

1.  Although not well documented, there is a filter called “style_loader_src”   We know that it exists because searching wp-includes folder for “style_loader_src” will show us the following code in class.wp-styles.php …

apply_filters on style_loader_src

apply_filters on style_loader_src

The fact that apply_filters() is calling it, means that we can call add_filter in our functions.php …

add_filter( ‘style_loader_src’, ‘remove_css_version’);

The first parameter is the filter “style_loader_src” and the second parameter “remove_css_version” is our arbitrarily named function which we have to define as follows (also in the functions.php file …

function remove_css_version($src, $handle) {
print_r($src);
}

How did we know that remove_css_version() should takes two params $src and $handle?  See the apply_filters call shown above.

In our function remove_css_version(), we put print_r of the $src to see what we have to work with.  We see that it gives us the css URL including all the query parameters.

Luckily, there is a wordpress function remove_query_args which helps us remove query args.  So we write …

add_filter( ‘style_loader_src’, ‘version_css_version’);
function version_css_version($src, $handle) {
return remove_query_arg(‘ver’, $src);
}

Now version stamp on stylesheets are removed.

To do same for javascripts, use filter script_loader_src.

To add timestamp version, do …

add_filter( ‘style_loader_src’, ‘version_css_version’);
function version_css_version($src, $handle) {
$src = remove_query_arg(‘ver’, $src);
return add_query_arg(‘timestamp’, date(“Y-m-d H:i:s”) , $src);
}

remove version and add timestamp

remove version and add timestamp

 


Related Posts

Tags

Share This