Remove Query Strings from Static Resources in WordPress

Remove Query Strings from Static Resources in WordPress

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 refers to the part a URL that carries content values or 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. Query strings URLs have “?” in their URLs structure.

In the above example, ?ver=4.4 and ?ver=5.2 are the query strings. In WordPress, it is generally implemented by developers to instantly render new updates 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 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 URLs that have query strings in their URLs.

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

Performance report with query strings

That’s why, query strings should be removed from all the static resources to enable browser caching. Caching the static resources can dramatically reduce the page load time, and it also reduces the load on server.

Remove Query Strings with Code

If you want to remove query strings with code, simply put the function in your theme’s functions.php, in your own plugin or in Functionality plugin, it’ll remove query strings from static resources.

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

After this, most of the query strings should be removed from static resources. But sometimes, developers use WordPress version number as query strings. To remove WordPress version strings, add the following function –

// Remove WordPress version strings from styles and scripts
function bydik_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( 'style_loader_src', 'bydik_remove_wp_version_strings' );
add_filter( 'script_loader_src', 'bydik_remove_wp_version_strings' );

Use both functions to completely remove query strings, including WordPress version strings. And if all goes well, you should no longer see the warning about query strings in website performance test tools.

Performance report without query strings

Remove Query Strings with Plugin

If you’re not familiar with code solution, you can also remove query strings by using plugins. There are many plugins can do that for you. We recommend to use Remove Query Strings From Static Resources plugin.

After activating the plugin, it should do the trick, there is no settings page for the plugin. In any problem, 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 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 your website address, please let us know. We will be happy to assist you!


Leave a Reply

Your email address will not be published. Your comments must follow our guidelines.