How To Remove Query Strings From Static Resources In WordPress

Home » WordPress Tutorials » How To Remove Query Strings From Static Resources In WordPress
  • query strings

How To Remove Query Strings From Static Resources In WordPress

If you use popular website testing sites like Pingdom and GtMetrix, you will often times run across the following message:

Remove Query Strings From Static Resources –  Resources with a “?” in the URL are not cached by some proxy caching servers. Remove the query string and encode the parameters into the URL for the following resources:

The warning comes with a very interesting recommendation. In layman’s terms, this means that some users will not be able to cache your static resources (images, css, js, etc) if they are using certain proxy servers. However, 99% of users will still be able to cache your resources, so this recommendation is not always “bad.”

The warning only improves the load time for a small subset of your potential user base. Query strings are not that bad. They allow you to bust users cache in the event of updates to provide a more reliable user experience when you change your website’s design.

How to Remove Query Strings

If you still want to remove query strings from your static resources, add the following piece of code to your functions.php file.

/**
* Remove query strings from static resources
*
* @version 1.0.0
* @since 1.0.0
* @return string Cleaned script URL
*/
function sert_script_version( $src ){
$parts = explode( '?ver', $src );
return $parts[0];
}
if ( ! is_admin() ) {
add_filter( 'script_loader_src', 'sert_script_version', 15, 1 );
add_filter( 'style_loader_src', 'sert_script_version', 15, 1 );
}

Important Note: This code will only apply to the front end of your website. The WP-Admin panel will still be served the query strings. (This prevents many issues involving WordPress and plugin updates.)

I recommend contacting a developer such as myself, for five minutes of my time, if you have questions about removing query strings.

By | 2017-12-17T23:45:56+00:00 December 15th, 2017|Categories: WordPress Tutorials|Tags: , |0 Comments

About the Author:

Scott Hartley is a WordPress Developer who focuses on website performance optimization, SEO, and security. He can't tell you what a color wheel is but he knows the intricacies of making a website load quickly.

Leave A Comment