Remove Query Strings from Static Resources

When it comes to improving WordPress performance and speed, one of the more commonly asked questions by more experienced users is, can query strings be removed from static resources?

The answer to that is yes, they can. Usually, the JS and CSS files for your WordPress website will contain the version of the file at the end of the URL, for example, ‘ ver=4.5’.

Some servers can’t cache a query string even if the header, cache-control: public is there and this means that getting rid of these strings can provide some small measure of improvement to caching which, in turn, speeds up your website a little.

If you use GTMetrix or Pingdom, sometimes you will see a warning pop up that tells you to remove these strings, so that’s what we are going to do here.

WordPress developers often use file versioning as well so that, when you get around to updating your plugins, you won’t need to flush out the cache.

For example, if a developer releases an update that changes style.css? ver=4.5’ to ver=4.6, it gets seen as a brand-new URL and, as a result, does not get cached.

Removing a Query String from a Static Resource

You can do this in a couple of ways, one way with a plugin and the other using code. If you make use of a CDN for delivering your content, you might not need to do this; some CDN providers already include query string caching as part of their service.

Do check with your provider and with your host before you do any of the following, to see if they already do it for you or can help you do it.

Using a Plugin

Using a plugin is by far the easiest way to remove query strings and the best is called Query Strings Remover. It is an incredibly popular plugin and it will take out all query strings, including “&” and “?” from your URLs in any of your CSS, JS and other static resources.

Download it from WordPress, the same way as you do any other plugin, install it and away you go. You don’t even need to configure it. DO clear your cache after you have installed it though, to make sure you can see the changes.

Alternatively, you could use another popular WordPress plugin called Remove Query Strings from Static Resources, which works the same way as the previous one. Do make sure you only run one of these plugins at a time.

Using Code

Not everyone wants to add another plugin to their WordPress website; after all, too many of these don’t do your website performance any favors either.

In that case, if you are happy and experienced enough to change the functions.PHP file for your WordPress theme then you can do it with code.

A word of warning here; do not attempt this if you have never done this before or are not comfortable; doing it wrong could break your website!

Locate the fucntions.PHP file in your WordPress theme and add this code:

‘function _remove_script_version( $src ){
$parts = explode( ‘?’, $src );
return $parts[0];
add_filter( ‘script_loader_src’, ‘_remove_script_version’, 15, 1 );
add_filter( ‘style_loader_src’, ‘_remove_script_version’, 15, 1 );’

If you do it right and you don’t break your website, you won’t get that warning in Pingdom or GTMetrix and your website should be that little bit faster.

Leave a Comment Below