Categories: How-ToWordpress

Remove query strings from static resources on WordPress

Remove query strings from static resources on WordPress

Since page loading speed becoming more and more important for extending your google page rank,caching your blog pages on wordpress is a must and should be taken very seriously. But before you can cache content on your WordPress blog you need to prepare files to get cached successfully.

One part you could do and i describe here is caching your css and js files which you should deliver always staticly. But the main problem of caching these links is the version tag at the end of each file link, cache mechanism won’t just recognize it as cachable content. To prevent loading static content like css and js files dynamicly we have to get rid with the version tag “?ver=x.xx” by Remove query strings from static resources on WordPress Links.
In this short tutorial i will describe a possible solution for wordpress and show you how to do this automaticly by strip off these version tags out of your links to get a better result in caching.

 
I hope you already have created a child-theme out of your primary theme template. This should be preferred because changes you apply to your files won’t get overwritten on next Theme update.

All we are going to do now is extending your functions.php with a specific replace code.

So open your functions.php with your preferred file editor and add the following code at the end of the file:

// remove version query string by https://ispire.me
function _remove_query_string( $src ){
        $parts = explode( '?ver=', $src );
        return $parts[0];
}
add_filter( 'script_loader_src', '_remove_query_string', 15, 1 );
add_filter( 'style_loader_src', '_remove_query_string', 15, 1 );

After applying this codeset, wordpress is replaces each occuring “?ver=” tag automaticly.

Have Fun with it!

Jules

Jules is the owner and author of ISPIRE.ME. He's a Linux System Engineer, Tech fanatic and an Open Source fan.

View Comments

    • Nowdays i wouldnt recommend remove query strings from static resources anymore. GTMetrix e.g. should revise their complaints about it.
      Its more important to have a cache busting feature for .js and .css files by appending query strings than serving outdated static files to clients due long persistent user caches which can lead into malformed page renderings

Recent Posts

HTTP/2 SSL Offloading with Hitch and Varnish

HTTP/2 SSL Offloading with Hitch and Varnish Since Chrome browsers showing you insecure warning on…

7 years ago

File changed as we read it GlusterFS issue

File changed as we read it GlusterFS issue Recently i had trouble running Backups of…

8 years ago

Running multiple instances of varnish using systemd

Running multiple instances of varnish using systemd If you have not yet found a complete…

8 years ago

HTTP/2 SSL Offloading with Haproxy and Nginx

HTTP/2 SSL Offloading with Haproxy and Nginx After HTTP/2 becoming more an more prominent regarding SSL…

8 years ago

Get Real IP with Haproxy Tomcat Jira Confluence using x-forwarded-for

Get Real IP with Haproxy Tomcat Jira Confluence using x-forwarded-for Everyone knows the Problem. Get…

8 years ago

Review TDS2 How to backtest using tick data with Metatrader 4

Review TDS2 How to backtest using tick data with Metatrader 4 in this Review TDS2…

8 years ago

This website uses cookies.