Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Created February 16, 2017 23:33
Show Gist options
  • Save tzkmx/f50281bf9d78250e312bec594b77a138 to your computer and use it in GitHub Desktop.
Save tzkmx/f50281bf9d78250e312bec594b77a138 to your computer and use it in GitHub Desktop.
Preserve cache busting without query strings for WordPress

With this simple plugin, your URLs will be stripped of their query string, but the version is preserved to allow for cache busting.

However you need instruct your webserver to strip the version added in order to let clients use the files.

    location ~ /Versioned/ {
        rewrite ^/Versioned/([^/]+)(/.*)(\.[0-9a-zA-Z]+)$ /$2$3 last;
    }
<?php
// put this in your must-use plugins path
function _remove_query_strings_1( $src ){
$host = $_SERVER['HTTP_HOST'];
$new = preg_replace( "/^(http|https:\/\/)({$host})\/([^?&]+)([&?]+ver=)(.*)$/", "$1$2/Versioned/$5/$3", $src);
return $new;
}
if ( !is_admin() ) {
add_filter( 'script_loader_src', '_remove_query_strings_1', 15, 1 );
add_filter( 'style_loader_src', '_remove_query_strings_1', 15, 1 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment