How to remove query string from static Resources?

Wondering how to remove query string from static resources? Just place these code in your functions.php file.

function sasweb_remove_query_strings_1( $src ){
$rqs = explode( '?ver', $src );
return $rqs[0];
}

if ( is_admin() ) {
// Remove query strings from static resources disabled in admin
}

else
{
add_filter( 'script_loader_src', 'sasweb_remove_query_strings_1', 15, 1 );
add_filter( 'style_loader_src', 'sasweb_remove_query_strings_1', 15, 1 );
}

function sasweb_remove_query_strings_2( $src ){
$rqs = explode( '&ver', $src );
return $rqs[0];
}

if ( is_admin() ) {
// Remove query strings from static resources disabled in admin
}
else {
add_filter( 'script_loader_src', 'sasweb_remove_query_strings_2', 15, 1 );
add_filter( 'style_loader_src', 'sasweb_remove_query_strings_2', 15, 1 );
}

This code will remove query strings from static resources like CSS & JS files, and will improve your speed scores in services like PageSpeed, YSlow, Pingdoom and GTmetrix.

Resources with a “?” or “&” in the URL are not cached by some proxy caching servers, and moving the query string and encode the parameters into the URL will increase your WordPress site performance significant.

About the Author

dr.code.skm

I am backend developer with passion in Web Application development using latest technologies like Laravel, PHP7, React, ECMAScript 6 and WordPress. I prefer spending time analyzing big data with Apache Spark. Apart from that, I do photography.