Learn how to remove version query strings from CDN URLs in WordPress to prevent file loading issues. This guide provides a simple code snippet for smoother CDN integration.
Some CDNs choke on query strings. WordPress adds a ?ver= tag to every script and style URL, and a few CDNs treat that as a separate, uncacheable request. If your assets aren’t caching the way you expect, stripping that version string is one thing worth trying.
Removing File Version from CDN URLs
Hook a small function to the script_loader_src filter and drop the ver argument when the URL points at your CDN:
/**
* Remove the file version query string from CDN URLs.
*
* This function removes the 'ver' query string from any asset URL
* that is served from a specific CDN. This is useful when the CDN
* has issues loading files with version numbers.
*
* @param string $src The original asset URL.
* @return string The asset URL without the version query string.
*/
function theme_name_cdn_version_remove( $src ) {
// Check if the URL contains the CDN domain
if ( strpos( $src, 'cdn_name_here.com' ) ) {
// Remove the version query string ('ver') from the URL
$src = remove_query_arg( 'ver', $src );
}
return $src;
}
// Apply the filter to remove the version query string from script URLs
add_filter( 'script_loader_src', 'theme_name_cdn_version_remove', 9999 );
How It Works
- Check CDN Domain: The function only touches URLs on your CDN. Swap
cdn_name_here.comfor your real CDN domain so you don’t accidentally strip versions from assets served elsewhere. - Remove Version Query:
remove_query_arg( 'ver', $src )pulls the?ver=1.0style parameter off the URL and hands back a clean one. - Filter Applied: The
add_filter()call runs this on every script URL throughscript_loader_src. CSS runs through a separate filter, covered below.
Additional Option: Apply to Both JavaScript and CSS
Scripts and styles use different filters, so add the same callback to style_loader_src to cover your CSS too:
add_filter( 'style_loader_src', 'theme_name_cdn_version_remove', 9999 );
One honest caveat
That ?ver= string isn’t just noise, it’s cache-busting. When you ship a CSS or JS change, WordPress bumps the version so browsers and proxies fetch the new file instead of a stale cached one. Strip it and that safety net goes with it, so after an update some visitors can keep loading old assets until their cache expires. The cleaner fix is fingerprinted filenames (like app.abc123.css), where the name itself changes on every build. If your CDN genuinely can’t cache query strings, this snippet is a fair workaround. Just know the trade you’re making. Drop it in your theme’s functions.php or a small plugin.
Next: WooCommerce vs Shopify


