In efforts to increase the performance of a WordPress website, tools like Google Page Speed and Yahoo YSlow may direct you to remove the query string from JavaScript and CSS files. Most scripts and stylesheets called by WordPress include a query string identifying the version. This can cause issues with caching and such, which will result in less than optimal load times.
For instance, jQuery.js may show up like:
[shell]/wp-includes/js/jquery/jquery.js?ver=1.4.2[/shell]
Thanks to an idea from this post, there is a very simple function that can be added to remove all query strings from script and css files.
In your theme’s function file (Appearance > Editor > Functions.php), add the following code before the closing PHP tag (%>) at the bottom of the file:
[php]
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 );
[/php]
Hope you find this helpful!
That’s a cool. Do you know how to remove it from just a specific js file?
For instance, I actually want the query stings on locally hosted JS for versioning. BUT, for jquery, I serve google’s hosted version b/c it’s more likely to already be cached in a viewer’s browser–more likely that is without the query string.
I found out how on stackoverflow. http://stackoverflow.com/questions/2514079/wordpress-problem-with-wp-enqueue-script
add_filter(‘script_loader_src’, ‘toscho_script_loader_filter’);
function toscho_script_loader_filter($src)
{
if ( FALSE === strpos($src, ‘http://ajax.googleapis.com/’) )
{
return $src;
}
$new_src = explode(‘?’, $src);
return $new_src[0];
}
Wow. Found another way. The WP docs don’t explain this very well. Change the second value from ‘false’ to ‘null.’ and it removes the version string.
ex:
(“http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js”), false, null);
I’m lost. Where is this line found? I don’t see it in the previous code you posted nor in the original code posted by John.
Nice shortcut. Thanks.
Thanks for having this in your blog, was great help for me on my blog, will make sure to remember this one. :)
Thanks… I Have Succesfully Modified Minify CSS For W3 Total Cache
Simple, functional and useful. Thank you! Something like I was looking for about 2 hours. Keep it up :o).
I’m glad you found it useful. Thanks for the feedback.
Dude thanks alot for this code, I was looking for a way to remove those version info from my css and js scripts for my optimization to work. :) Your code perfectly works. Cheers
Cheers for that, been looking for this for over an hour now, thank you :).
Nice share, Very helpfull for me. And IT works like a sharm.
I’ve included this in my functions file, but it’s just not working. if you use pingdom to check the site: nintendoconnect.com … there are so many plugin versions that are still being queried. Any thoughts?
hi
thanks for your awesome post
but there is a problem ! .. the pictures
some of my pictures have query string
but what can i do ?
Its very help full on Remove query strings from static resources of Google Page speed service suggestion.
Thanks
Thankyou very much. I’ve successfully removed the js and css query strings with your script.
Hi John,
Thanks for the info but it didn’t work for me…I’m using W3TC and it is pointing to my slides on my home page. It caches whatever slide is loading at the time of the test. I would like to remove the query string? from the jpeg to help my Google Page Speed. Any suggestions would be appreciated.
Hi John, Thanks for the code, I have just applied to my function.php file and it worked for me but still I am getting this error for another files like,
http://tricksmachinetest.tm.netdna-cdn.com/wp-content/themes/freshlife/includes/js/custom.js?84cd58
How to remove that ??
Thanks.
@john awesome tutorial man. U made my life easier.
@Vishal, Did u fix the problem?
tried to put the code into my functions.php but it breaks!!
Parse error: syntax error, unexpected T_STRING in /home/deb52593/domains/catterycelizes.nl/public_html/wp-content/themes/atahualpa/functions.php on line 975
so it won’t work for me
thanks alot for this code… :)
Great code man..thanks
Hey there,
I have put this code at the bottom of my function.php and nothing happen? What am i doing wrong? I still have like 20 links with the query string.
If u have problem using the code, use this one:
function hide_wp_version($src) {
global $wp_version;
return str_replace(“?ver=$wp_version”, ”, $src);
}
add_filter(‘script_loader_src’, ‘hide_wp_version’);
add_filter(‘style_loader_src’, ‘hide_wp_version’);
Just add to your “function.php” file… Tested :)
Or you can simply use WordPress built-in function
remove_query_arg( ‘ver’, $url );
I updated the function.php and it worked great. So far nothing seems to be broken :)
It worked for the theme files linking to CSS and JS, however for external files it didn’t work:
http://cdn.zopim.com/?Y0us6LKMmX1znPBSvEg3v5rrQy2aHKeP
http://maps.googleapis.com/maps/api/js?sensor=false
http://static.ak.facebook.com/connect/xd_arbiter.php?version=18#channel=f25a1c089c&origin=http%3A%2F%2Fwww.firepips.c¢¦
https://s-static.ak.facebook.com/connect/xd_arbiter.php?version=18#channel=f25a1c089c&origin=http%3A%2F%2Fwww.firepip¢¦
These files still the same as before. Any suggestions?
This code will break any site that enqueues a script that requires querystring parameters, like the Google Maps API. That’s a lot of potential breakage. It would probably be safer to limit that changes to scripts hosted on the local domain, though there’s no guarantee they won’t require querystring parameters. Maybe best would be to only remove the WordPress-specific “ver” parameter.
How do I limit this so it does not affect the googlemaps API?
Hi John. How do you do this on drupal? Thanks.
Thank John, your solution has worked really well. Thank a lot. My website is: http://theonlinecasinowebsites.com
Wow! This helped me a lot! Thanks! :D
John,
Thanks for sharing! This worked perfectly in WordPress 3.7 with the Genesis framework.
I spoke too soon. It actually breaks a few CSS calls for Google fonts. I should be able to tweak it. Thanks for getting me started!
Jay
Hey Jay,
Were you able to work this out? I am still looking for a solution.
I added this code to my finction.php file (but I’m not exactly sure I put it in the correct place). Since then there has been a text string showing at the very top of my web page. The text string is the exact code I added to the function.php file. So I assumed that was the problem. I removed the code from the file, published, and reviewed my site.
But it still exists on my site even after clearing history and cache.
Seems very strange to me. Any ideas?
I used this on my directory and broke the Google Maps API request for the sensor. It wouldn’t recognize the option for true or false. Work great if not using any API services though :)
Didn’t work. The query tags are still showing if I’m using W3TC. :(
Hi John! If my wordpress theme doesn’t have a specific functions.php folder, can I place this function in any of the php folders? I will be so happy if this works!
The functions.php file goes in the root directory of the theme (the same directory “style.css” is located).
Thanks John,
This helped to some extend but not completely. I have added this to function.php of my website mycitymychoice.com but when i check through GTMetrix I still see around 20 entries against “Remove query strings from static resources” and score is only 84. What can I do to reduce it further?
Thanks
Avinash – http://mycitymychoice.com
Thanks John Chapman, It worked for me.
Thanks for that pice of code.
Thank you for this information and sharing it with us! After adding the php in my functions.php file, Pingdom is still giving me a 13/100 score for removing query strings from static resources. Could that mean that my W3TC is not configured properly?
I truly appreciate any help!
loadtime has dropped from >4s to <1.4s .. . thank you for this priceless tip.
What i want to know, why doesn't WordPress include this code as standard??
There is also a plugin for wordpress that has the same effect, I wonder if it simply injects this code ?
I found this code too aggressive: it also removed Google assets, e.g. fonts.
Amend the function code to:
[code]
if( strpos( $src, ‘?ver=’ ) )
$src = remove_query_arg( ‘ver’, $src );
return $src;
}[/code]
I can suggest that this one is very good, and because no settings or database used
https://wordpress.org/plugins/remove-query-strings-littlebizzy/
only can by defined constant if you wish custom… thanks bro