WordPress Tutorial

How to Remove Query Strings from Static Resources in WordPress

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.

What Is a Query String?

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.

Why Remove Them?

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.

Remove Query Strings from Static Sources in WordPress

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.

Removing Query Strings from Static Sources with Code

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.

  1. On your WordPress Dashboard, navigate to Appearance -> Theme Editor -> Theme Functions.
  2. Copy and paste the following code into the functions.php file. Once you are finished, click Update File.
// 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 );

Using Plugins to Remove Query Strings from Static Sources

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.

  1. On your WordPress admin page, go to Plugins -> Add New. Look for Speed Booster Pack, then install and activate it.
  2. Navigate to the Speed Booster menu and enable Remove query Strings in the General tab. You can also activate other optimization options if you want. Click on Save Changes when you’re finished.
    This is how to remove query strings from static resources using Speed Booster Pack

To prevent your WordPress site from ever creating query strings again, you can use W3 Total Cache.

  1. Install and activate W3 Total Cache. Go to its settings through the Performance menu.
  2. Select Browser Cache and check the Prevent caching of objects after settings change option. Once that is done, don’t forget to hit Save all settings.
    How to prevent query strings from appearing again using W3 Total Cache

Good job, you have removed query strings and stopped them from appearing again!

Testing the Result

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:

Test result in GTMetrix before we remove query strings

Here’s one after we removed query strings:

The GTMetrix result after we removed query strings from static resources

Conclusion

Dynamic resources use query strings to help web servers identify different assets 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:

  • Editing functions.php file — locate your functions.php file through Appearance -> Theme Editor -> Theme Functions. Insert the provided code to the functions.php file as shown previously.
  • Using plugins — install Speed Booster Pack and activate Remove query strings option from the General tab. Using W3 Total Cache plugin, go to the Browser Cache menu and uncheck the Prevent caching of objects after settings change option.

About the author

Irfan F.

Irfan is a member of the Digital Content Writer team at Hostinger. He believes that technology and the internet can help improve our lives significantly. That's why he shares his experience as a WordPress blogger to educate others. In his free time, Irfan enjoys good films and books.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!

More in WordPress Tutorial
How to Customize the Excerpt Length in WordPress
Close