Removing query string is very easy in WordPress. But first of all we should know why remove the query string from static resources. It helps us to improve the caching of site because Some servers and proxy servers are unable to cache query strings When you test your page with GTmatrix or Pingdom, you will be suggested to remove query string from static resources. So today we will learn that how to remove query string from static resources in WordPress.
Example: http://mydomain.com/wp-content/plugins/js/abc_js?ver=3.4.1
Only gaining points on GT matrix or page speed test should not be our main aim. Query strings are not always bad. Query strings define the version of plugins and themes. It is very necessary for automatic update of plugins and core. But in some cases many proxies do not cache resources with a “?” in their URL.
How to remove Query Strings from static resources in WordPress?
We can remove the query string in WordPress by the following ways:
1. Remove Query String from Static Resources with Code
2. Remove Query String from Static Resources with Plugin
1. Remove Query String from Static Resources with Code
If you have some knowledge of development you can remove query string with few lines of code.
Add the following code to your themes function.php file inside the PHP tags.
function _remove_script_version( $src ){
$parts = explode( '?ver', $src );
return $parts[0];
}
add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
After adding the above code, Save the file and clear the cache if you are using any caching plugin. Now check your page source and all the query strings will be removed from the CSS and JS file URLs.
2. Remove Query String from Static Resources with a Plugin
There are many plugin available at WordPress repository to remove Query string in WordPress. If you are using caching plugin like W3 total cache, there is inbuilt functionality of query string removal. We can remove query strings by combining the CSS and Js files.
If you are using W3 Total cache plugin, Wprocket or any others optimization plugins, It will be done easily by adding your js and CSS files in Minify option. This plugin combines all CSS and JS files into one CSS and JS file and the new output file has no query string.
So you can use any one method for removing the query strings from static resources.