Remove Query Strings From Static Resources on WordPress

Did your Website Speed analysis Test throws an alert to “Remove Query Strings from the Static Resources”? If Yes, then you are landing on the right Place. Most of the Visitors like you landed this page after testing their website on Pingdom, GTmetrix, YSlow or Google Pagespeed Insights, finding solutions to Remove query strings from static resources like CSS & JavaScript etc. Now-a-days, after the Google Panda update, Website Loading Speed is Considered as a significant factor of Search Engine Ranking. Lower is the Website loading time, better will be the Search Engine Ranking. Caching of Static Resources like CSS and JavaScript plays an important role in page loading time. Query Strings prevents Caching of Static Resources on Proxy servers and on browsers. By Removing Query Strings from Static Resources you can enable caching of static resources and can achieve a significant improvement in page load speed with less page load time. In order to boost the website loading time, there are several factors that plays crucial role. Among those factors, “Removing the Query Strings from the Static Resources” is also a very important factor. If you did not check your website yet, I recommend to go GTmetrix and check your website. Try to Correct those alerts and you will see a massive improvements on Page Loading Time of your website, hence Search Engine Ranking.

So Today in this Post, I’ll discuss:

  • What are the Query Strings in WordPress code structure?
  • Why you should Remove Query Strings from Static Resources?
  • How you can Remove Query Strings and Increase your WordPress Website Speed?

WHAT ARE THE QUERY STRINGS IN WORDPRESS?

Remove query strings from static resources

Remove query strings from static resources

Have you ever seen a URL that are appended with ‘?’ or ‘&’ ?? For Simplicity, Query Strings Resources are the URLs with a “&” or “?” at the end of the URL. They can be seen on images, CSS and JavaScript in most cases. Most plugins affix them to their URL’s. Most of the proxy servers do not cache CSS and JavaScript resources with Query Strings even if you have optimized Public Cache Control headers.

So normally, a URL having Query Strings will look something like this:

http://sitename.com/wp-content/plugins/js/some_js?ver=3.4.2

WHY YOU SHOULD REMOVE QUERY STRINGS FROM STATIC RESOURCES?

As I have already explained that the Query Strings prevent caching of Static Resources like CSS and JavaScript on Proxy Servers and CDN . This will results into slow loading of the Website. So in order to fasten up the Website loading speed, we somehow must enable caching on Proxy Servers. This could only be Possible if we remove the Query Strings (which content “?” or “&” in their URLs) from the static Resources. And once you remove these strings, this will reflect in your increased page loading speed.

HOW YOU CAN REMOVE QUERY STRINGS AND INCREASE YOUR WORDPRESS WEBSITE SPEED?

I will Provide three Solutions to Remove Query Strings from Static Resources on WordPress.

  1. Using Plugin. [Recommended]
  2. Using W3 Total Cache. [Recommended]
  3. By editing Theme’s  functions.php file. [Partial recommendation; only if you know what are you doing.]

#SOLUTION 1 : REMOVE QUERY STRINGS FROM WORDPRESS USING PLUGIN

Query Strings from Static Resources can be removed by using a WordPress plugin called Remove Query Strings From Static Resources. The Plugin is very small in size, so that you can use the plugin without any worry. Just install the Plugin & Activate it, and you are good to go!

This is Probably the best solution if you are a newbie, or don’t want to change or mess up with the existing theme Code. I also recommend this solution because if you add Codes on Theme’s core files, it will remove upon updating/upgrading the Theme. So it’s better to use the minimal sized plugin.

#SOLUTION 2 : REMOVE QUERY STRINGS USING W3 TOTAL CACHE PLUGIN

So, What Cache Plugins are you using? I highly recommend you to use W3 Total Cache plugin to improve/decrease the Page loading time. This plugin is a well-developed and coded, which dramatically decrease your website’s loading speed. And if you are not using this plugin, I would really suggest you give it a try, you won’t regret using it.

W3 Total Cache Plugin Provides an Option to Remove the Query Strings from the Static Resources. Follow these 3 simple steps to Remove the Query Strings:

  1. Go to “Browser Cache” tab on W3 total Cache Plugin.
  2. Find “Prevent caching of objects after settings change” option under General Settings. UNCHECK it !
  3. Save settings. Now ‘Empty all Cache’  &  you are done.

#SOLUTION 3 : BY EDITING THEME’S FUNCTIONS.PHP

This Option is recommended for Pro Users. Use it only if you know what are you doing. Just Copy the Following code and paste it at the last of the functions.php file (before closing php tag).

/*** Remove Query String from Static Resources ***/
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 );

CONCLUSION:

Removing Query String from Static Resources like CSS & JavaScript is important, if you want to enable Caching of Static Resources on Proxy Servers and want to increase your website page loading speed. I have chosen the #Solution 3 (editing functions.php). After Removing the Query Strings from Static Resources on WordPress , I found the dramatic improvements of Page speed Grades on GTmetrix and Pingdom.

That’s all. Hope you find the Post Useful. Do share your Page Speed Grades, views & experiences regarding the above topic. And also, share which of the above method did you applied to Remove Query Strings from Static Resources on WordPress. 

Reply