How to Remove Query Strings from Static Resources in WordPress

Posted on

by


In web development, while we check out or analyze our website in GTmetrix and Pingdom somewhere it shows us to remove query strings from static resources like CSS, JavaScript and JQuery and all kinds of files.

What is Query String?

Query string is a part of a link that includes data to be processed by query programs to retrieve specific data from the database. Query strings contains ‘?’ or some characters ahead to it. In a link symbol question mark separates link and query string from each other so that it can be processed further.

In WordPress the version of static files are shown as a query string.

Example: http://www.gethow.org/static.css?ver=1.0

Some time query strings are not helpful. While caching things cache programs rejects to cache files when it has a query string. Suppose if our whole static files are with query strings then almost nothing is cached. So removing it is necessary.

How to Remove Query Strings from Static Resources?

You can overcome this issue either via using just a simple plugin or you can add few lines of code in your theme file. Both the ways are as follows, prefer any one you like and use it.

Method 1 – Use Query Strings Remover Plugin

If you are not geeky enough then you can use simple plugin named Query Strings Remover to solve this issue. This plugin do not use even 0.01% of your server resources.

Download Query Strings Remover and Install it.

Note: After installing it no configurations are needed so just relax and let the plugin do its job automatically. We suggest you to use this method because if you change your theme in future the plugin will still do its job for new theme and again no configurations are needed.

Method 2 – Add Few Lines of Code in Your Theme

To remove the query strings from static resources just open your theme and find the function.php file and paste the below code at the end of all codes. This code will remove all the query strings from static resources.

function qsr_remove_script_version( $src ){
    $parts = explode( '?ver', $src );
        return $parts[0];
}
add_filter( 'script_loader_src', 'qsr_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', 'qsr_remove_script_version', 15, 1 );

Note: Make a backup of your theme template first before editing it. (If you are newbie in coding things)

Note: If you are using this method then note that if you change the theme you have add this code again in your new theme; because the code gets disabled or removed with your theme when you disable or delete the theme.

Now clear the cache if you are using cache plugin and then check again your website on GTmetrix and Pingdom. You will see the issues of query strings in static resources are gone.

Read related contents by similar tags: