How to Remove Query Strings from Static Resources with PHP?

Categories WordPress TutorialsTags , , , 3 Comments

When you test your website’s performance by different tools or website like Google, GTMetrix, Pingdom, KeyCDN etc., you must have seen a status named “Remove query strings from static resources“. To speed up your WordPress website’s performance, you need to remove query strings from static resources.

What are Query Strings?

The query string is the part a URL that contains content values or carry the version details. For a WordPress website, it generally carries version numbers of static resources like images, javascript (.js), stylesheets (.css) etc. and sometimes it carries WordPress version number also.

http://example.com/wp-content/themes/theme-main/style.css?ver=4.8
http://example.com/wp-content/plugins/jetpack/css/jetpack.css?ver=5.1

Generally, query strings URL’s have “?” in their URL’s. In the above example, ?ver=4.8 and ?ver=5.1 are query strings. In WordPress, it is generally implemented by developers to instantly render new updates. Developers use query string as it is not cached by most of the browsers and CDN services.

Why Query Strings Should be Removed?

Although query strings might be helpful for web developers and designers but query strings have a great impact on your website’s loading speed. Almost all the search engines, browsers, web hosts nowadays do not index or cache the resources or URL’s that have “?” in their URL’s.

With query strings, you will see bad results in website’s performance testing tools or website. Below is an example of performance result in Pingdom with query strings.

Remove Query Strings - Low Performance

Caching reduces the load on server. So caching the static resources can dramatically reduce the page load time. To enable caching, query strings should be removed from all the static resources and wherever possible encode the parameters into the filenames itself.

Remove Query Strings by Function

If you want to remove query strings with code, simply add the function in themes functions.php, your own plugin or Functionality plugin. It’ll remove query strings from static resources.

// Remove query strings from static resources
function sm_remove_cssjs_ver( $src ) {
    if( strpos( $src, '?ver=' ) )
        $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'sm_remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'sm_remove_cssjs_ver', 10, 2 );

After this, most of the query strings will be removed from static resources. But sometimes developers use WordPress version number as query strings. WordPress version strings may not removed by using the function. To remove WordPress version strings, add the following function.

// Remove WP version strings from scripts and styles
function sm_remove_wp_version_strings( $src ) {
    global $wp_version;
    parse_str(parse_url($src, PHP_URL_QUERY), $query);
    if ( !empty($query['ver']) && $query['ver'] === $wp_version ) {
        $src = remove_query_arg('ver', $src);
    }
    return $src;
}
add_filter( 'script_loader_src', 'sm_remove_wp_version_strings' );
add_filter( 'style_loader_src', 'sm_remove_wp_version_strings' );

Use both functions to completely remove query strings. And if all goes well, you should no longer see a warning about query strings in website performance test tools such as Pingdom.

Remove Query Strings - High Performance

Remove Query Strings by Plugin

You can also remove query strings by using plugins. There are many plugins can do that action. We recommend to use Remove Query Strings From Static Resources plugin. After activating the plugin, it should work.

Let us know in comments, what you have done and how it goes for your website!

3 Comments on this.

  1. I used this code on my site. But, still, some query strings are left. I noticed that most of them are from Jetpack plugin. Any idea, what to do?

    • Jetpack loads additional resources from WordPress.com servers. Those functions remove query strings from files located at your website server, not from third-party servers. You can’t remove query strings from third-party sources.

      If those file sources pointing you website address, please let us know.
      We will be happy to assist you!

Leave a Reply

Your email address will not be published.