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:00December 15th, 2017|Categories: WordPress Tutorials|Tags: , |2 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.

2 Comments

  1. Tito October 2, 2018 at 12:15 am - Reply

    Why does everyone recommend same code snippet? And about plugin so much easier isn’t?
    https://wordpress.org/plugins/remove-query-strings-littlebizzy/

    • Scott Hartley October 2, 2018 at 11:07 am - Reply

      Not quite the same snippet you find elsewhere this excludes the admin JS to avoid issues in future updates, with plugins like yoast who might not versions JS files / css files in the name. This snippet is slightly different.

      Additionally, many plugins might add junk to the admin panel whether it’s an extra menu option and instead want a simple snippet that doesn’t need updating. Sure I could modify the code to be broader but certain themes/plugins include query strings to differentiate files and other issues could arise.

Leave A Comment

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