Remove Query Strings from Static Resources

%%title%% %%sep%% %%sitename%%
Remove Query Strings from Static Resources

Learn how to remove query strings from static resources in WordPress for better caching and performance. Improve your site's speed and SEO by following this simple code guide.

Run a WordPress site through GTmetrix or Pingdom a few years back and you’d get scolded for one thing: those ?ver=1.0.0 tags hanging off your CSS and JavaScript URLs. The advice was always the same, strip them out. If you still want to do it, here’s the snippet that does it with no plugin. But read the note at the bottom first, because this advice has aged.

The code

Drop this into your active theme’s functions.php. It hooks the two filters WordPress runs every asset URL through, script_loader_src and style_loader_src, and chops off everything from the ?ver onward.

/**
 * Remove query string from static resources.
 */
function remove_query_strings() {
   // Check if admin backend.
   if( ! is_admin() ) {
       add_filter( 'script_loader_src', 'remove_query_strings_version', 20 ); // Remove query string of JS static resources
       add_filter( 'style_loader_src', 'remove_query_strings_version', 20 ); // Remove query string of CSS static resources
   }
}

/**
 * Hooks for splitting the URL to query and return only the static resource URL.
 *
 * @param string $src The original source URL of the asset.
 * @return string The URL without query strings.
 */
function remove_query_strings_version( $src ) {
   $arr = preg_split( "/(&ver|\?ver)/", $src );
   return $arr[0];
}
add_action( 'init', 'remove_query_strings' );

The is_admin() check keeps this on the front end only, so the editor and dashboard still load their assets normally. Everything else just trims the URL back to its base path.

Before you use this

Here’s the honest part. This tip is mostly obsolete. It came from an era when some proxies and CDNs refused to cache URLs that carried a query string. Modern caches don’t care, they’ll happily cache style.css?ver=1.0.0 like any other URL, so the performance win you’re chasing usually isn’t there anymore.

Worse, that ?ver= is doing a real job. It’s cache busting. When you ship a CSS or JavaScript update, the version bump forces browsers to fetch the new file. Strip it away and returning visitors can keep serving a stale copy from cache until they hard refresh. That’s a genuine tradeoff, not a free score boost.

If your real goal is clean, cache-friendly asset URLs, the right fix is fingerprinted filenames, where the file itself is named after a hash of its contents (app.a1b2c3.js) so a new build changes the name and busts the cache automatically. Most build tools do this for you. Reach for the snippet above only if a specific tool is still nagging you and you understand what you’re giving up.

Next: How to Fix “Uncaught TypeError: Cannot read property ‘msie’ of undefined jQuery”

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top