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.


Tristan Whittaker

Founder & Director, Infinity3

Tristan Whittaker is the founder and director of Infinity3, with more than 20 years of experience in web design, digital marketing and technology. He works with businesses across the UK to improve their online presence, generate more opportunities and make better use of digital tools, AI and automation.

Combining strong technical knowledge with a practical understanding of how businesses actually operate, Tristan has helped hundreds of organisations create better websites, improve their visibility and simplify the systems behind them. He’s particularly interested in finding sensible ways to use technology to solve real business problems, without unnecessary complexity or jargon.