Remove Query Strings From Static Resources

< 1 min read

If you’ve done a page speed test on a website like GT Metrix, you might have come across a suggestion to remove query strings from static resources. If you right click > view source on your website, you may well see a number of URLs for your css or javascript files similar to http://www.mydomainname.com/wp-includes/js/jquery/jquery.js?ver=1.10.2.

The problem with these query strings is that many proxies do not cache resources with a “?” in their URL so it’s a good idea to remove these query strings from references to static resources. Removing them is actually pretty straight forward:

1) Locate your themes functions.php file. You can normally find this file if you navigate to “Appearance” > “Editor” and look in the list of list on the right hand side.

2) Copy and paste this code into your functions.php within the php tags.

function _remove_script_version( $src ){
    $parts = explode( '?ver', $src );
        return $parts[0];
}
add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );

3) Save your functions.php file, refresh your website and view the source again. This time you should see that the query strings have been removed.