Remove Query Strings from static resources in WordPress

Remove Query strings from static resources in WordPress is a very easy process. You need just few lines of code but what is exactly the problem? Most proxies, do not cache resources with a “?” in their URL even if a Cache-control: public header is present in the response. To enable proxy caching for these resources, remove query strings from references to static resources, and instead encode the parameters into the file names themselves.

Problem

/wp-includes/js/jquery/jquery.js?ver=1.1.2
/themes/one-minimal/style.css?ver=3.5" type="text/css" media="all"

Fix in WordPress, just add the code below in functions.php

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

Result

<script type='text/javascript' src='http://ageorgiev.com/wp-includes/js/jquery/jquery.js'><script>
<link rel='stylesheet' id='one-minimal-style-css'  href='http://ageorgiev.com/wp-content/themes/one%20minimal/style.css' type='text/css' media='all' >

Now go and check your score in GTmetrix or Google PageSpeed Insights!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.