How to remove query strings from static resources?

How to remove query strings from static resources?
4.2 (83.33%) 12 votes

The page load time is essential for any website. It enhances user experience as well as search engine rankings of your site. However, various aspects can effect website load time. One such aspect is query strings from static resources. Do you want your WordPress website to perform well on the platforms such as Google Page Speed Insights, YSlow, Pingdom, or so on? Perhaps, by removing query string from static resources, you can tackle this problem. In this article, I will be presenting you the ways through which you can remove query strings. But before we head to removing them, it is important to know more about query strings.

What exactly are query strings resources in a WordPress website?

In a WordPress site, query strings resources are those URLs that have “&” or “?” character in them. Even after optimizing Public Cache-Control Headers, Proxy servers often fail to cache CSS and JavaScript resources having Query strings. Additionally, some of the CDNs also fail to cache resources with Query Strings. You can test or check the source code of your website through tools like Yslow and GTmatrfix.
Removing Query strings from static resources

There are two solutions for removing Query strings. In the theme’s file “function.php”, you can add a PHP code for removing it. While you can also add a plugin to your WordPress website to get it removed. It is also an easy method to get this task done. Let’s look at both of the methods in details.

By adding plugins

You can easily remove query strings from static resource quickly by adding a WordPress plugin to your WordPress website. There are various plugins available in the market that can get this task done such as Remove Query Strings from Static Resources, Query Strings Remover, Speed Booster Pack, etc. This solution is ideal especially if you aren’t familiar with PHP coding.
Theme updates won’t affect this feature, but even though theme updates remove it, then you can easily get it back through the plugin.

By adding PHP code

If you are pretty familiar with PHP coding, then adding PHP code can be an ideal method to remove query strings. In your function.php file, you have to add some codes. You can do this through online File Manager or FTP. You can also use your WordPress Dashboard for adding code, but it’s better if you use FTP or File Manager. Nevertheless, you shouldn’t forget to back up the function.php file before modifying it, so that you can get back the original website even if you get messed. You need to have cPanel access for accessing function.php file through online File Manager. Now, copy-paste the following code in function.php file at the bottom.

/*** For removing Query Strings ***/
function remove_cssjs_ver( $src ) {
if( strpos( $src, '?ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}

add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );

Conclusion

If you are a paid theme user, then you should better go for the plugin. It is because theme publisher often releases theme updates and you might lose the PHP code while updating your paid theme. Here we have ways to remove query strings from static resources. Hopefully, you will implement one of the methods on your WordPress website and get rid of query strings.

Leave a Comment