WordPress automatically appends a ?ver= query string to every enqueued CSS and JavaScript file. The value is typically the WordPress version number, the plugin’s version, or a custom version set by the developer. The intent is cache-busting, when you update a file and bump its version number, browsers and CDNs see a different URL and load the fresh version instead of a cached copy.
The problem is that many older CDN configurations and reverse proxies, including some shared hosting setups, refuse to cache URLs that contain query strings at all. For these environments, the ?ver= parameter actively prevents caching rather than enabling it, which means assets are fetched fresh on every page load instead of being served from edge cache.
The Code
Add this to your functions.php or a site-specific plugin. It applies to both stylesheet and script URLs via WordPress’s style_loader_src and script_loader_src filters, running at priority 9999 to process after all other filters have run.
How It Works
The function receives the full asset URL as a string and checks whether it contains ver=. If it does, WordPress’s built-in remove_query_arg() function cleanly strips it from the URL. The result is a clean URL like /wp-content/themes/my-theme/style.css instead of /wp-content/themes/my-theme/style.css?ver=6.4.2.
When to Use This
This snippet is most beneficial on sites using a CDN or reverse proxy that doesn’t cache query string URLs. Bunny.net, for example, can be configured either way, check your Pull Zone settings under “Disable Query String Sorting” and caching rules before applying this snippet to confirm it will actually improve your cache hit rate.
It’s also commonly applied on sites targeting perfect scores in tools like GTmetrix or Google PageSpeed Insights, where cacheable resources are flagged based on URL cleanliness.
Things to Keep in Mind
Removing version strings means you lose the automatic cache-busting mechanism. If you update a theme file or plugin and the URL stays the same, browsers may serve the old cached version to returning visitors until the cache naturally expires. To mitigate this, you can manually purge your CDN cache after deployments, or use filename-based versioning (e.g. style.2.css) for your own theme assets instead of relying on query strings.
This filter also applies to WordPress core assets, third-party plugin assets, and everything else enqueued through the standard WordPress queue. If a specific plugin breaks after applying this snippet, you can whitelist that asset’s URL with a conditional check inside the function.
add_filter( 'style_loader_src', 'nsl_remove_version_strings', 9999 );
add_filter( 'script_loader_src', 'nsl_remove_version_strings', 9999 );
function nsl_remove_version_strings( $src ) {
if ( strpos( $src, 'ver=' ) !== false ) {
$src = remove_query_arg( 'ver', $src );
}
return $src;
}
