How to Remove Query Strings From Static Resources in WordPress

Written on September 02, 2016. Updated on November 09, 2017

When it comes to WordPress performance, this question comes up quite a bit, and that is how to remove query strings from static resources. Your CSS and JavaScript files usually have the file version on the end of their URLs, such as domain.com/style.css?ver=4.6. Some servers and proxy servers are unable to cache query strings, even if a cache-control:public header is present. So by removing them, you can sometimes improve your caching. This will also fix that warning you see in Pingdom and GTMetrix called “Remove query strings from static resources.”

Remove query strings from static resources

Remove query strings from static resources

Versioning on files is also used by WordPress developers so that when you update a plugin, you aren’t forced flush the cache. For example, if they push out an update and change style.css from ?ver=4.6 to ?ver=4.7, it will be treated as a completely new URL and won’t be cached. They are also used for organization in development workflows.

Remove Query Strings From Static Resources

There are a couple different ways you can remove query strings, one is with a WordPress plugin and another is with code. If you are using a CDN to deliver your assets, this might not be required as some CDN providers actually have the ability to cache query strings. Check with both your web host and CDN provider prior to implementing the following to see if they can cache query strings.

  1. Remove Query String From Static Resources With Plugin
  2. Remove Query String From Static Resources With Code

1. Remove Query Strings From Static Resources With Plugin

One of the easiest ways to remove query strings from static resources in WordPress is to use the free Query Strings Remover plugin.

remove query string from url-plugin

Query Strings Remover WordPress plugin

As of writing this, the plugin has over 10,000 active installs with a 4.5 out of 5 rating. The plugin removes query strings such as “?” or “&” in your URLS from all of your static resources, such as CSS and JavaScript. You can download Query Strings Remover from the WordPress repository or by searching for it within your WordPress dashboard under “Add New” plugins.  The great thing about it is that there is no configuration necessary. Just install and your good to go. Make sure to clear your cache after installing though to see the changes. You can see a before and after below.

With Query Strings (before plugin)

query strings

Requests with query strings

Without Query Strings (after plugin)

no more query strings

Requests without query strings

Another alternative plugin that is also popular is the free Remove Query Strings From Static Resources plugin. As of writing this, the plugin has over 90,000 active installs with a 4.6 out of 5 rating. You don’t need to run this and the above plugin, only one or the other, as they both essentially do the same thing.

And a final alternative would be to use the premium perfmatters plugin (developed by a team member at Kinsta), which allows you to remove query strings from static resources with a click of a button. It also enables you to easily implement other optimizations for your WordPress site, and works alongside your current caching plugin.

Remove query strings with perfmatters plugin

Remove query strings with perfmatters plugin

2. Remove Query Strings From Static Resources With Code

You can also remove query strings from your assets with code. Simply add the following to your WordPress theme’s functions.php file.

Important! Editing the source code of a WordPress theme could break your site if not done correctly. If you are not comfortable doing this, please check with a developer first.
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 );

And if all goes well, you should no longer see a warning about query strings in website speed test tools such as Pingdom (as seen below).

remove query strings from static resources warning

Fixed Remove query strings from static resources warning

Was this article helpful?
No, or there was something off

Hand-picked related articles

Send this to a friend