How to Remove Query Strings From Static Resources in WordPress

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 www.prado.lt/style.css?ver=4.9.5. 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 GTmetrix and Pingdom called “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 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.

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).

Leave a Reply

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