Header Text - Speed Up WordPress by Removing Query Strings

When your WordPress website loads, it pulls in many files, including stylesheets, JavaScript, and images. These are called static resources. Sometimes, you’ll see extra text at the end of those file URLs, like ?ver=1.2.3. That part is called a query string, and it usually tells the browser which version of the file to load.

The issue is that many caching tools and content delivery networks don’t cache files properly if they have query strings. That can slow your site down, especially for repeat visitors. Instead of saving those files for quicker access, the browser loads them several times.

That’s where this tutorial comes in. Here, we explain how to remove query strings from static resources on your WordPress site. Doing this can boost your site’s speed, help with caching, and even improve your score on speed testing tools.

KEY TAKEAWAYS

  • Remove query strings from URLs to make your WordPress site load faster and improve caching.
  • Query strings often are similar to ?ver=1.2.3; these are added to CSS and JS files for version control.
  • Some caching tools and CDNs don’t cache files with query strings.
  • You can manually clear the query by adding a code snippet to your theme’s functions.php file or using a plugin.
  • Always back up your site before making code changes to avoid problems.
  • After removing query strings, test your site to see if you’ve achieved speed improvements.

What is a Query String?

A website Uniform Resource Locator (URL) is made up of different parts:

  • The protocol (like HTTP or HTTPS)
  • The domain name
  • And the path.

These parts work as a team to help your browser find and load the correct web page.

However, sometimes, you’ll see an additional parameter in a URL, called a query string, that comes after a question mark (?). This is used to pass extra information to the browser. In WordPress, query strings often appear at the end of file URLs and usually include a version number. This tells the browser which version of the file to load.

For example, a URL like this:

/wp-includes/js/jquery/jquery.js?ver=1.12.4

has a query string at the end, like ?ver=1.12.4 and tells you the file is version 1.12.4 of jQuery. WordPress adds this automatically to let browsers know when a file has changed. That way, users get the latest version without using cached files.

So, query strings help with version control. But while they’re useful for updates, they can sometimes create issues with caching, which is why many people choose to remove them. We’ll explain more about that in the next section.

Why Remove Query Strings from Static Resources?

Numerous things highlight the significance of removing query strings from static resources in WordPress. First, it can make your WordPress site faster and easier to cache. That’s because some servers, CDNs and proxy tools won’t cache files that include query strings. When these files aren’t cached, your site reloads them every time; this slows things down, especially for returning visitors.

Removing query strings helps caching tools store your static files, such as CSS and JavaScript. This leads to better loading times and can improve your scores on tools like Google PageSpeed Insights, Pingdom, and GTmetrix. A faster site means a better user experience and lowers bounce rates.

Removing the query string from the URL improves the website’s SEO because search engines prefer web URLs that are simple and easy to crawl. While query strings don’t always hurt rankings, removing them can improve how your site is indexed.

There’s also a small security benefit. Query strings often include version numbers, which reveal what WordPress version or plugin version you’re using. Hiding these details makes it harder for hackers to target known bugs in older versions.

So, if you’re trying to boost performance, improve SEO, and tighten security, remove query strings.

2 Ways to Remove Query Strings from Static Resources in WordPress

There are two ways to clear query strings:

  1. By writing code in your theme’s functions.php file.
  2. By using a WordPress performance or caching plugin.

Remember, if your site uses a Content Delivery Network (CDN), you may not need to do a thing, because some CDNs automatically cache query strings. But if not, follow one of the methods below to clean them up.

Remove Query Strings Using Code

Before you start editing code, it’s a good idea to backup your website. If something goes wrong, you’ll have a working version you can restore. Once done with this, go through the following steps to remove query strings from static resources:

Login to your WordPress Admin Dashboard panel. Go to Appearance Theme File Editor. On the right, find and click functions.php.

Once the file is open, copy and paste the following code:

// Remove query strings from static resources in WordPress
function remove_query_strings_from_static_resources( $source ) {
    if( strpos( $source, '?ver=' ) )
        $source = remove_query_arg( 'ver', $source );
    return $source;
}

add_filter( 'style_loader_src', 'remove_query_strings_from_static_resources', 15, 1 );
add_filter( 'script_loader_src', 'remove_query_strings_from_static_resources', 15, 1 );

This code checks if a file URL contains ?ver= and removes it. It works for stylesheets and JavaScript files.

After pasting the code, click Update File to save changes.

Remove Query Strings From Static Resources - Edit functions.php File

Then, clear your WordPress site’s cache and reload your website. Here’s how URLs appeared before clearing the query string:

Remove Query Strings From Static Resources - URLs Before Removing Query String

Here’s how the URLs look after your edit functions.php:

Remove Query Strings From Static Resources - URLs After Removing Query String

You can also check your website with tools like GTmetrix to ensure the query strings are gone. Just follow the steps given below:

  1. Login to the GTmetrix account.
  2. Fill in your website’s URL and click Analyze.
  3. Switch to the Waterfall tab.
  4. Hover over the static resources’ links to see if the query strings are removed.
Remove Query Strings From Static Resources - Confirm Query String’s Removal Using GTmetrix

Note: If you’re using a block theme (e.g., Twenty Twenty-two) or don’t have access to the WordPress dashboard, edit the functions.php file using your hosting account’s File Manager. Once in File Manager, go to public_htmlwp-contentthemes. Open your active theme’s folder, locate functions.php, and open it in an editor to add code. However, if you’re a Hosted.com client, refer to our knowledgebase guide to find out how to access and use the Hosted.com File Manager.

Clear Query Strings Using Plugins

If you’re uncomfortable editing theme files and writing code, use a performance or caching plugin to remove query strings from static resources. There are multiple plugins; however, ensure you use one with good reviews and compatible with your WordPress core and theme.

For this section example we use one performance plugin and one caching plugin to demonstrate how to clear the query string from static resources in WordPress:

  • USE SPEED OPTIMIZER PLUGIN

The Speed Optimizer is a comprehensive performance-boosting plugin. Here’s how to use it:

Go to WordPress Dashboard Plugins Add New and search for Speed Optimizer. Once it appears, click Install Now and then Activate.

Remove Query Strings From Static Resources - Install Speed Optimizer Plugin

Once activated, go to Speed Optimizer Frontend in the left-hand menu of your dashboard. Then, switch to GENERAL.

Remove Query Strings From Static Resources - Access General Settings of Speed Optimizer Plugin

Find the option labeled Remove Query Strings from Static Resources and toggle it on.

Remove Query Strings From Static Resources - Enable Remove Query String Feature
  • USE W3 TOTAL CACHE PLUGIN

W3 Total Cache is a powerful plugin designed to boost your website’s speed and performance, helping with SEO and Core Web Vitals. It reduces load times using modern optimization methods and supports CDN integration for faster content delivery. Here’s how you can use it to remove query strings from static resources:

First, install and activate this plugin (we discussed this process in the previous section). Next, go to Performance Browser Cache.  Under the General, CSS & JS, and Media sections, check Remove query strings from static resource.

Additionally, ensure you enable Prevent caching of objects after settings change under the mentioned sections to prevent WordPress from adding query strings again.

Then, click Save Settings to apply your changes.

Remove Query Strings From Static Resources - Enable Remove Query String from Static Resources Using W3 Total Cache Plugin

After that, navigate to Performance Purge All Caches from the toolbar menu to clear all caches. This is important to ensure all old file versions with query strings are removed and new optimized versions are served.

If you don’t purge, you may still receive cached files with ?ver= parameters, making optimization ineffective. A full cache reset guarantees consistent performance improvements across your site.

Remove Query Strings From Static Resources - Clear All Cache Using W3 Total Cache
Strip Banner Text - Get faster load times with LiteSpeed-optimized WordPress Hosting. [Learn More]

FAQS

Will removing query strings break my website?

No, it usually won’t break anything. But to be safe, back up your site before making changes. WordPress will still load your CSS and JS files without the version tags, and your site should work fine.

Do I still need to remove query strings if I use a CDN?

Not always. Some CDNs cache files with query strings work without any problem. But if you’re unsure, it’s still a good idea to remove them to ensure your files are fully cacheable and your site loads faster.

Is removing query strings good for SEO?

Yes, cleaner URLs without query strings can help search engines crawl and rank your site better. While it’s not a major SEO factor, it can improve overall site performance, which affects SEO rankings.

Do all performance plugins remove query strings?

Not all. Some performance plugins include this feature, while others do not. Ensure the plugin you choose – like Speed Optimizer – has a remove query strings option.

Can I re-add query strings if I change my mind later?

Yes. If you use a plugin, turn the setting off whenever you want. If you added code manually, remove or comment out the code from your functions.php file. Then, clear your site cache to apply the changes.