If you’ve checked how fast your website is on tools like Pingdom or GTmetrix, you might have seen a suggestion to take out query strings from your static stuff. Although there are good reasons to use query strings, you might want to remove them to make your site load faster.
Here’s our guide to getting rid of query strings from static things, with or without using a plugin.
How to Remove Query Strings from Static Resources with a Plugin
A lot of plugins that help your site work better have a choice to take out query strings from things that don’t change, like W3 Total Cache and a plugin I really like, WP Rocket. Let me show you how to use these plugins to remove query strings from static things.
Using W3 Total Cache to Remove Query Strings from Static Resources
If you’re using W3 Total Cache, you can stop using query strings from static things by changing a setting in the browser cache section. To do this, just go to Performance and then Browser Cache
Scroll down a bit, and you’ll find a choice called “Remove query strings from static resources.” Put a checkmark in the box next to it, and then click on the “Save” button!
Using WP Rocket to Remove Query Strings from Static Resources
If you’re using WP Rocket and you want to take out query strings from static things, here’s what you do:
Go to your WP Rocket settings by clicking on “Settings” and then “WP Rocket.” In this area, you’ll see a tab called “File Optimization” on the side.
When you’re in the “File Optimization” tab, you’ll find a choice that says “Remove query strings from static resources.” Put a checkmark in that box, then save your changes. After that, make sure to clear your cache so the change starts working.
Remove Query Strings from Static Resources using Plugin
If you’re not using a plugin that helps with performance and query strings, don’t worry. There are plugins made especially for removing query strings from static things.
The simplest way is to install a plugin called “WP Remove Query Strings From Static Resources” from WordPress.org.
This plugin makes your website faster and improves your scores in speed tests like Pingdom, YSlow, PageSpeed, and GTmetrix.
If you’re aiming for a perfect score of 100 in Google’s PageSpeed test, you can follow this complete guide.
Once you’ve installed and turned on this plugin, you can check if it’s working by looking at the code of your homepage. You’ll see that extra things added to your CSS and JS files are now gone.
If you’re also using a plugin that stores copies of your pages to make them load faster, you might want to clear that cache too.
How to Remove Query Strings from Static Resources without a Plugin
If you’d like to take out query strings from static stuff using code, this little piece of code here will help you do that! You can put this in your theme’s functions.php file or use the Code Snippets plugin to add it.
<?php //Remove Query Strings From Static Resources function da_remove_query_strings_from_static_resources( $src ) { if( strpos( $src, '?v=' ) ){ $src = remove_query_arg( 'v', $src ); } if( strpos( $src, '?ver=' ) ){ $src = remove_query_arg( 'ver', $src ); } return $src; } add_filter( 'script_loader_src', 'da_remove_query_strings_from_static_resources', 999 ); add_filter( 'style_loader_src', 'da_remove_query_strings_from_static_resources', 999 );
The PHP code I showed will go through your scripts and styles and get rid of the ‘ver’ and ‘v’ query strings in them.
Just like that, your static things won’t have those query strings anymore!
However, be careful and make sure your site still works right after you make this change. It can cause problems if you’re using a CDN. Usually, files with a query string are seen as new files by a CDN. This helps make sure new versions of files are used instead of old ones (because they have the same name without a query string).
Related Articles
Leave a Reply