How to remove query strings from static resources in WordPress and speed up your site

If you have been trying to speed up and optimize your WordPress site, you have probably come across different online diagnostic tools that can help you with that. We encourage you to do the tests; they will tell you how well your website is performing, and most of those tools will show you details about the speed of your site.

For example, if you run a test on GTmetrix, it will show you which images are slowing down your site. It will tell you the loading speed of style sheets and scripts, details about minification, and much more. One of the things that many WordPress users find on top of the list (meaning that the problem should be addressed as quickly as possible) is “remove query strings from static resources”. In this article, we will briefly explain what query strings are and how to quickly remove them in order to improve the speed of your website.

What are query strings

Query strings are parts of URLs that you can find after a question mark (?) or ampersand (&) symbol. Browsers or external applications usually add these parts to URLs in order to add additional parameters.

By using query strings, for example, a WordPress plugin can pass different values through the URL so that other applications and services can use them.

https://firstsiteguide.com/?utm_source=twitter&utm_medium=cpm&utm_campaign=twitter

More often than not, after clicking on an advertisement, article from social media or your email inbox, you will see a quite lengthy URL with query strings. In order to tell analytics services the origin of your visit, other services (and humans themselves) will add UTM – specific query strings that describe sources, media, and campaigns. Marketers use this to track the effectiveness of their campaigns. And this is something completely normal; don’t worry about these kinds of query strings.

Why and when should you remove query strings?

GTmetrix remove query strings from static resources

While query strings are an important part of dynamic elements, they can slow down your WordPress site if they’re used in static resources. If you’ve ever opened a browser console while on your site, you may have found that the website is showing version numbers of scripts and styles. This might look familiar:

/wp-includes/css/dashicons.min.css?ver=4.4.2

Some proxy caching servers and CDNs can’t cache these kinds of resources. That means that each time you open a site, the server will have to send you the file over and over again. And that’s something that slows down a site tremendously. Just that one query string won’t make much of a difference, but when you pile up a few of them, things can get serious. It’s finally time to resolve the issue and remove query strings from static resources.

How to remove query strings from static resources

Query String

There are a few techniques to remove query strings from static resources in WordPress, and we’re about to show them to you. You can do that by copying a simple function, or you can use a few WordPress plugins that will resolve the problem for you. Whatever technique you choose, you won’t be wrong – you will remove query strings from static resources in the next few minutes and speed up your site the very moment.

Add a simple function

The first method requires you just to copy and paste a simple function to your functions.php file. If you have ever added a function there, you already know it’s not that big of a deal. But if this is your first time, please take extra caution since one little mistake in the file can render your site unusable. Just to feel safe, please create a backup of the file before you proceed.

  1. Open and edit functions.php file
  2. Copy and paste the following code:
  3. 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 );
  4. Save changes

Uncheck the option in W3 Total Cache

Since W3 Total Cache is a popular plugin which counts more than one million active installs at the moment, chances are that you are already using the plugin. In that case, you will just need to uncheck one option from the settings:

  1. Navigate to Performance -> Browser Cache
  2. Uncheck the Prevent caching of objects after settings change option
  3. Save changes

Use Remove Query Strings From Static Resources plugin

If you don’t like adding the code to functions.php by yourself, you can use a free plugin for the job. Remove Query Strings From Static Resources is a lightweight & straightforward plugin that will do the same thing without requiring you to touch the code.

Just install the plugin, activate it, and you’re done. The moment you activate the plugin, the query strings will be removed from static resources.


No matter the technique you used, removing query strings shouldn’t have been a difficult task. Now that you’re done, you can run another scan on the speed & performance tool of your choice to see if and how the score (and the speed of your site) improved.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

One thought on “How to remove query strings from static resources in WordPress and speed up your site

  1. […] How to remove query strings from static resources in WordPress and speed up your site […]

Leave a Reply

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