HomeMethod

Knowledge

Web VitalsTechniquesSnippetsAnswersImpactsBottlenecks

Remove query strings from static resources

  • Performance

Some CDNs and proxy caches treat URLs with query strings as uncacheable. WordPress appends ?ver=X.X to every CSS and JS file, which can prevent these caches from storing your assets.

remove-query-strings.php
<?php
/**
 * Remove Query Strings from Static Resources
 *
 * Strips version query strings (?ver=X.X) from CSS and JS URLs. Some CDNs
 * and proxy caches refuse to cache URLs with query strings, so removing them
 * can improve cache hit rates. The trade-off is that you lose automatic
 * cache busting on version updates — only use this if your CDN handles
 * purging or you use filename-based versioning.
 *
 * Usage: Add to functions.php.
 *
 * @category performance
 * @language php

 *
 * @author Adame Dahmani https://adame.io
 * @support Support my family project: https://adame.io/fundraiser
 */
function snippet_remove_query_strings( $src ) {
    if ( strpos( $src, '?ver=' ) !== false ) {
        $src = remove_query_arg( 'ver', $src );
    }
    return $src;
}
add_filter( 'style_loader_src', 'snippet_remove_query_strings', 10 );
add_filter( 'script_loader_src', 'snippet_remove_query_strings', 10 );

The trade-off: you lose automatic cache busting when plugins update. Only use this if your CDN handles purging on deploy, or if you use filename-based versioning.

Support our family now