How to fix “Remove query strings from static resources” in GTMetrix and PageSpeed

September 21, 2015
Posted in: Code Snippets, How To, Web Development, WordPress

When checking your website against Google PageSpeed Insights, GTMetrix, Pingdom, or any other major site optimization checker, you may run into “Remove query strings from static resources”.  This message appears because adding a querystring to any URL can cause proxies and caching mechanisms to ignore cache instructions, and always get the latest version of that file.

This is great if the file is constantly being updated, but since most static resources are unchanged for weeks at a time, it makes sense to take advantage of all caching available to us, and get a quick boost in speed for anyone visiting more than one page on the site.

A quick WordPress filter can be added to your functions.php file in your website’s theme:


function remove_script_version($src) {
  return remove_query_arg("ver", $src);
}
add_filter("script_loader_src", "remove_script_version", 15, 1);
add_filter("style_loader_src", "remove_script_version", 15, 1);

This quick bit of code will remove the "?ver=4.x.x" from the end of all scripts and styles loaded properly through WordPress.

Scott Buckingham

President / Owner
613-801-1350 x101
[email protected]
Scott is a WordPress expert who has worked on hundreds of web design and development projects. He excels at finding creative ways to solve technical problems. View full profile