Remove Version Query String From Static JS And CSS Files

How to Remove Version Query String From Static JS And CSS Files

in Optimization & Best Practice
Google+ LinkedIn

A website owner should always be trying to improve the speed of their website as much as possible. When you analyze your page using any page speed analyzer such as Page Speed, YSlow or Pingdom, you are very likely to see suggestions to remove query strings from static resources, such as Stylesheets and JavaScript files. This is a problem as many proxies will not cache the resources if it has a query string in the URL. A tip on the Google best practice pages is to remove all query strings from your static resources to enable proxy caching of the file. So in this article I will show you how you can easily remove version query string from static scripts and stylesheets.

Most of the stylesheets and JavaScript files added by WordPress plugins and themes usually have version query string (?ver=) in their URL. And we want to remove version query string from these static resources to make our pages and static resource to cache so that the pages can load faster. There are a couple of plugins in WordPress respiratory that does this job but this is an easy enough task to use plugin.

Just paste this following WordPress snippet in your theme’s functions.php file to remove version query string from static scripts and stylesheets resources. Once you are done adding this snippet in your theme, head back to analyze your page again and notice the difference.

// remove version query string from scripts and stylesheets
function wcs_remove_script_styles_version( $src ){
    return remove_query_arg( 'ver', $src );
}
add_filter( 'script_loader_src', 'wcs_remove_script_styles_version' );
add_filter( 'style_loader_src', 'wcs_remove_script_styles_version' );

Share the love

If you like this snippet, share it with others!

6 Replies on "Remove Version Query String From Static JS And CSS Files"

  1. Amar Ilindra

    Thanks, it worked like a charm. Tried more than 3 snippets and your worked for me

    1. Hi Amar,

      I am glad this snippet worked for you. :)
      Thank you for letting us know.

  2. Duncan Michael-MacGregor

    Thanks for this, it works perfectly. Much better than using yet another plugin!

    1. Thanks Duncan,

      I’m so glad you found it helpful!
      Thank you for stopping by.

  3. Nooby

    Didnt Work ;(

    1. Care to explain what exactly you did and what did you get?

Leave a Reply

Your email address will not be published. Required fields are marked *