September 30, 2019
3 min Read
Irfan F.
In this article, we are going to explain how to remove query strings from static resources in WordPress. By doing this, you can improve your site speed and deliver the best experience for your users.
Sometimes, you’ll notice an instruction to remove query strings when testing your site’s performance with GTMetrix or Pingdom.
What exactly are query strings? Simply put, it is a version of a file. You can define them as URLs that include symbols like “&” and “?”. For example:
https://www.hostinger.com/tutorials/wp-includes/js/jquery/jquery.js?ver=1.12.4
?ver=1.12.4 is a query string. Mainly, developers use query strings for the purpose of cache-busting. It’s a process of resolving cache issues by identifying file versions on a site. That way, browsers won’t mistakenly cache the wrong asset if there is a new one available.
Query strings are actually only important for dynamic resources because assets (multimedia, CSS, JavaScript files, etc.) are changing more often than in static resources. That’s why they need to be identified differently to avoid caching problems.
If you have static resources, you don’t really need query strings. In fact, they can prevent web servers from caching the data, thus potentially making your site slower.
Remember that a slow website can undermine your SEO performance and the overall user experience.
Therefore, it is highly recommended that you remove query strings from static resources in WordPress. We’ll show you how to do it.
In this guide, you will learn about two methods – one is by modifying the functions.php file and the second is by using WordPress plugins.
Important: We want to remind you to be careful when modifying the code. It is best to create a backup first in case something goes wrong.
// Remove query string from static files 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 );
There are times when the previous method results in an error. Maybe you are simply not comfortable with coding. If that’s the case, you should try installing Speed Booster Pack.
It’s an all-in-one WordPress plugin that helps you boost your website’s loading speed. One of its features is removing query strings from your page.
To prevent your WordPress site from ever creating query strings again, you can use W3 Total Cache.
Good job, you have removed query strings and stopped them from appearing again!
Now that query strings are removed, it’s time to check the performance of your site. We’ll be using GTMetrix to display the test result before and after the optimization.
Here’s an example before we applied the optimization process:
Here’s one after we removed query strings:
Dynamic resources use query strings to help web servers identify different asset versions from one another.
They might actually prevent your website from being cached properly, making it slower in the process. Therefore we suggest that you get rid of them to avoid any issues in the future.
To summarize, this is how to remove query strings from static resources in WordPress:
LEAVE A REPLY