Remove Query Strings from WordPress Resources

If you’ve analyzed your WordPress blog with services like GTmetrix or Pingdom Tools, you may have been advised to remove query strings from static resources. That is to remove the part of your resource string (URL) that indicates your WP / Theme / Plugin version number.

Their reasoning is that some Proxy Caching servers do not cache resources with a URL that contains query strings aka version numbers. If you are using Cloudflare then you do not need to do this since Cloudflare has an option that can be set to cache those files.

But if you still want to remove the version numbers to have a higher performance score on sites like Pingdom Tools and GTmetrix then follow the instructions below.

Remove Version Number from Styles and Scripts

From this:
/wp-content/theme/style.css?ver=4.8.1
/wp-content/theme/script.css?ver=4.8.1

To this:
/wp-content/theme/style.css
/wp-content/theme/script.css

Add this little code snippet to your child-theme’s functions.php

add_filter( 'script_loader_src', 'remove_resource_version', 20 ); 
add_filter( 'style_loader_src', 'remove_resource_version', 20 );

function remove_resource_version( $src ) {

	if ( is_admin() )
		return $src;

	$output = explode( '?ver=', $src );
	return $output[0];
}

Using (?ver=) instead of simply (?) to filter the query strings ensures that other resources like Google Fonts — which usually uses something like fonts.googleapis.com/css?family=font-name — don’t get cut off.

Share This Post

Leave a Reply

Please write your comments in english. I delete anything that even remotely resembles spam.

Note: When posting code, enclose it in pre and code tags.
e.g. <pre><code> Add code here </code></pre>