Easily Remove Query String From CSS & Javascript In WordPress

Want to score good in Google page speed test then you have to remove query string from all css and java script files in your wordpress website.

There are many different factors which affect your total page load time and Google page speed test consider all of those factors. According to Gtmetrix, removing query strings from static resources like style-sheet and java-script has the higher priority which is considered during the speed test.

How to remove query string from static resources

What Is Query String ?

Query string is a method by which different plugins pass the content values or carry the version detail. They are used in both java-script and style-sheets. Actually its one of the method implemented by developers to instantly render new updates.

Here’s the explanation. Let’s say you are a developer and you have created a plugin which has few css and js files. Now if anyone who is using your plugin and also using a CDN service then these static files ( both css and js files ) will be cached for weeks, months or for even one year.

Now if you push up any update to your plugin it will not be visible until the cached file expires. So developers use the query string as it is not cached by most of the browsers and CDN services. Overall it might be helpful for web developers and designers but is a bad practice and have great impact on your website’s loading speed.

How To Remove Query String From Static Resources

I have two different options for you. You can either chose to edit your theme manually and add the custom code to your function.php file. Or you can go with plugin which is as usual easy to install.

#1 Using Custom Code

You have to just place it in your current theme’s function.php file. You can either choose to edit your theme via wordpress dashboard or you can even use ftp or online file manager.

Simply navigation to your wordpress dashboard then to appearance. Then from on hover menu select editor. Now on the right hand side search for function.php and place the code at the bottom.

// Remove query string from static files
function remove_cssjs_ver( $src ) {
 if( strpos( $src, '?ver=' ) )
 $src = remove_query_arg( 'ver', $src );
 return $src;
add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );

With the help of this code ?ver string will be removed. It can be altered as per need. But before making any changes I will request you to create a backup of theme’s function file.

#2 Using A Plugin

It’s the easiest way to manage all types of ? queries. You don’t need to edit any file, not you have to create any backup. Just install remove query strings from static resources plugin and you are done.

If you are still facing any problem then you can contact me directly from comment section.

Share on Google+Share on FacebookTweet about this on TwitterPin on PinterestShare on StumbleUponBuffer this page
  • http://smallsignsjusdaica.com Ugo

    Thank you for the explanation and the code.
    I tried and it works. Significant improvement with Gtmetrix, BUT it messes completely my theme (Avada).
    Any idea why?

    • Vivek Kumar Poddar

      ” It messes completly ” What, I don’t think it gives any output.

  • http://optimizewordpress.org/ Ben Hanna

    This worked really well thanks, Ugo, if you are using the plugin it may be conflicting with other caching plugins use the code

  • http://www.smartonlineincomes.com solomon Mwale

    Thank you so much for your help I have been looking for this for along time now I greatly appreciate your help. Will be looking forward to getting more insight from you soon.

  • Dan

    The only problem is it breaks the theme “site title and tag line” in every theme i try.

    • Vivek Kumar Poddar

      I don’t understand why its breaking your theme. If you applied the code snippet properly. I will suggest using the free plugin listed in this post.

  • Sally

    Thanks much! Used your custom code in a test site. In pingdom/tools went from 86/100 to 98/100! Using w3 Total Cache (94/100). Got the error about removing query string. Added your custom code to the functions.php file and got 98/100.