How to Remove Query Strings From Static Resources

Anyone familiar with WordPress will probably not be new to the statement – “Remove query strings from static sources.”   It mostly comes up as warnings in Pingdom and GTMetrix as well as other related tools used in checking our website’s loading time. You mostly find file versions on the end of the URLs of CSS and JavaScript codes, thus making it impossible for specific servers and proxy servers to cache query strings, despite the presence of a cache-control:public header.  Thus, when you get rid of these cache query strings, you are improving your caching, and ultimately enhancing the loading speed of your website.  Furthermore, you are also closer to fixing other problems that may be contributing to the speed of your site. 

Although, the query strings are present in the system for a reason; to ensure that there is no problem with caching in events of updating style.css from one version to another.  If the caching process is not managed correctly, and the cached version of the updated plugin is allowed to keep running, it may break the website until the cache is flushed entirely or the expiration of the cached resource. Query strings are also known to assist in organizational workflow development. 

In this article, we will be discussing all the possible methods of removing query strings from static resources so that they do not affect the loading time of your website adversely.

Method #1    Removing using code

Using a code, you can conveniently remove query strings from your assets.  Locate your functions.php folder of your theme and include the code below.  Once added, save the file and clear the cache.  If you are skeptical about editing your source code of your WordPress theme, you can consult a developer.   

function _remove_script_version( $src ){

$parts = explode( '?ver', $src );

return $parts[0];

}

add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );

add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );

Method #2    Using Plugins

Several WordPress plugins can help you remove the query strings from static resources served from your domain.  Some of these plugins are discussed below:

a.    W3 Total Cache Plugin

If you are using this, check your browser cache settings, and uncheck “Prevent caching of objects after settings change.”

b.    Remove Query Strings From Static Resources Plugin

The plugin can be downloaded from your WordPress dashboard, under the “Add New Plugins” option. All you need to do is install and clear the cache after installation.  No configuration is necessary.

c.    Premium Perfmatters Plugin

A member of the Kinsta team developed this plugin, and it removes query strings from static resources just with a single click. With the plugin, you can also add other optimizations to your WordPress and is compatible with your current caching plugin.

d.    Query Strings Remover Plugin

Here is another plugin that removes query strings from your static resources such as JavaScript and CSS files.  After installing and activating the plugin, clear the cache.  This helps you to monitor how useful the plugin is in improving the overall performance of your website.

e.    5 Speed Booster Pack

One great thing about Speed Booster Pack is his multifaceted nature.  With the plugin, you can do several things, all of which will enhance your website’s performance and make it SEO friendly. While other plugins remove query strings from static resources, the 5 Speed Booster Pack works by:

  • Removing external render-blocking JavaScript and CSS present in the above-the-fold content
  • Transferring the scripts to the footer, which in turn enhances the speed of loading the pages
  • Minifying and inlining all CSS styles, while moving them to the footer to remove the external render-blocking CSS and enhance its delivery
  • Saving bandwidth and speeding up pages by lazy-loading images
  • Keeping the sizes of file smaller by changing the compression level of images
  • Loading CSS asynchronously which ensures that your page loads faster and scores high on top speed testing services

It is vital to ensure that your website loads fast, as this affects the search engine ranking of your website significantly.  It also influences the kind of experience users to get when they visit your site.  A slow website is a turn off for most visitors. Thus it is essential that you check all the contributory factors to a slow website and have them fixed.  One of these is the removing query strings from static sources, whose various probable solutions have been discussed in this piece.

Good luck!

5/5 - (6 votes)