How to: Remove Query String URL From Static Resources in WordPress?
We have experienced in WordPress that it generates automatically query string URL by adding a “?” question mark inside of a URL. Which is somewhat not cacheable by most proxies and notably squid up through version 3.0 even if a cache-control:public header is present in response. What actually I mean is that in order to speed up a website we need it to be cleaned up from untidy query string URLs… Let me tell you a little bit more about those files which are being caused with query string URL. Mostly these files are CSS or JS…
Here are some examples about query string URLs:
http://1.gravatar.com/avatar/da2d44be2b1df905b5e297f7ee754c4f?s=65&d=&r=G https://softstribe.com/wp-content/plugins/wp-table-reloaded/css/datatables.css?ver=1.9.4 https://softstribe.com/wp-content/plugins/wp-table-reloaded/css/plugin.css?ver=1.9.4
You can see at the end of each URL mentioned above has a question mark and now the next parameters after “?” in the URL will be termed as query string URL.
How to Remove Query String URLs?
In this article I’m going to show you how to remove query string “?” from static resources especially. What we are going are to do is actually adding simple line of codes inside of our functions.php file before the closing php tag ?>.
function _remove_script_version( $src ){ $parts = explode( '?', $src ); return $parts[0]; } add_filter( 'script_loader_src', '_remove_script_version', 15, 1 ); add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
Save the file and try testing it again… Hope you will find your website cleaned by 100%.
Gday,
I have stumbled across this site in the last moments before giving in for the night and you saved me! Thankyou! I put a fresh jug on and will continue into the early hrs now.:)
One huge problem I have is that whenever I try to apply that fix for the query strings the clients whole site comes tumbling down. Luckily I have been burnt enough that I made a copy of the original first.
Any suggestions? It is a particularly problematic wordpress theme kallyus, but I am so close to getting it to a mid 90’s score that I just won’t be satisfied until I rule out every possibility.
Anyway, consider me a regular here from now on,
andY
I always welcome to my new visitors like you @andY, I will do my best to keep you with up to date WordPress tutorials on my blog. By the way, keep visiting us…
Has anyone else tried this? Looking for some more feedback before i try it. This has been hurting google page speed grade for a while.
No men, give it a try it’ll not hurt your website speed and by the way. Which caching plugin are you using so I can tell you in details about whether you should try or not.
Is this method still applicable if Im already using seo plugin by Yoast? Thanks Muhammad. And by the way, your site is great!
Hey @Mark, thanks for admiring my site. I’m pleased to read your precious comment. And by the way, this method is still acceptable to well known web browsers…
amazing i got 99 99 on gtmetrix by doing this still 1 error of removing query string from wp minify plugin
Doesn’t work for me. Everytime I add it to my functions.php code it either breaks the site or completely alters the fonts on the page. I’m using Genesis Epik theme.
If you are using Genesis Framework, then you should use this plugin, it also focuses to do the same as above code does.
Used it for my site http://estateagentsuk.co.uk but didn’t effect Pingdom results.
Any solution?
You can hire me @Steve in order speed up your site here:
https://www.microlancer.com/explore/wordpress-customization/8502-speed-up-wordpress-website
Looking forward to your reply.
I tried this script and I get A91 there is still some line to remove ! and I don’t know how to complete it.
Any tips for adding this to the vertex theme?
Try Autoptimize plugin that will combine all css and js files which will remove the ?v=3.3.1 from the links and there will no any query strings in your site.
Hope that sounds good?
This code is not working for me, Please check it.
It should work… What’s the error?
The same is happening where it is not removing the query strings completely, what’s happening is that after the javascript query it is adding a period.
ex. ‘yith-woocommerce-order-tracking/assets/js/ywot.js.’
Not possible. Which WordPress version is that?
Hi
If you are using worpress multisite, it will remove query strings on the main site but not the other sites even if you post it in every theme functions.php.
For me only worked on site 1 out of three sites.
I am using WP 4.4 (latest)
Worked for me. Thanks
I am using the latest version of wordpress