Remove query strings from static resources WordPress

Remove query strings from static resources WordPress is a major concern of web site optimization. Once development complete next thing is the optimization. Most of the developers using Pingdom tools, gtmetrix, yslow, google page speed insight and etc for test there web site performance. Once test complete list of performance insights will prompt. You can follow each to get an optimized web site. Most common suggestion by these tools is Remove query strings of static resources. Lets see how to remove query string from static resources.

Why we need to remove query string?

When optimization, caching is a main concern. If query strings available cache plugins can’t cache the CSS and JS files. Due to they have their own versions time to time. That is why we need to remove query strings from the static resources. We can do this in many ways but the ultimate goal is same. Anyhow you need to use the same filter.

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

Some codes throughout the internet has check for query string ( ? ) only. But I am not recommending that way. Take a look at below URL.

//fonts.googleapis.com/css?family=Roboto:400,500

I used Google Roboto font for one of my project. If you notice here all Google Font styling passing after the query string (?). Assume if you’re removing rest from query string (?) as per the example I took here, Roboto font styles will remove completely. So, you can’t see any font family is working and it’ll switch to next or default font. Other hand you might have a question why this Google Font style enqueuing from function file without importing from the stylesheet. Of course you can import from the Stylesheet. But it is not recommended way to do. Either way is working but when you import through the Stylesheet it’ll decrease your site loading time than you’re enqueuing it.

Hope you enjoyed this article. If you have questions regarding the post do comment below.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *