[WordPress] Remove Version Query Strings From JavaScript JS and CSS Stylesheet Files

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:

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:

Hope you find this helpful!

37 Comments

  1. 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.

    Reply
  2. 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);

    Reply
    • 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.

      Reply
  3. Nice shortcut. Thanks.

    Reply
  4. Thanks for having this in your blog, was great help for me on my blog, will make sure to remember this one. :)

    Reply
  5. Thanks… I Have Succesfully Modified Minify CSS For W3 Total Cache

    Reply
  6. Simple, functional and useful. Thank you! Something like I was looking for about 2 hours. Keep it up :o).

    Reply
    • I’m glad you found it useful. Thanks for the feedback.

      Reply
  7. 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

    Reply
  8. Cheers for that, been looking for this for over an hour now, thank you :).

    Reply
  9. Nice share, Very helpfull for me. And IT works like a sharm.

    Reply
  10. 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?

    Reply
  11. 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 ?

    Reply
  12. Its very help full on Remove query strings from static resources of Google Page speed service suggestion.

    Thanks

    Reply
  13. Thankyou very much. I’ve successfully removed the js and css query strings with your script.

    Reply
  14. 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.

    Reply
    • @john awesome tutorial man. U made my life easier.

      @Vishal, Did u fix the problem?

      Reply
  15. 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

    Reply
  16. thanks alot for this code… :)

    Reply
  17. Great code man..thanks

    Reply
  18. 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.

    Reply
  19. 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 :)

    Reply
    • Or you can simply use WordPress built-in function

      remove_query_arg( ‘ver’, $url );

      Reply
  20. 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?

    Reply
  21. 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.

    Reply
    • How do I limit this so it does not affect the googlemaps API?

      Reply
  22. Hi John. How do you do this on drupal? Thanks.

    Reply
  23. Wow! This helped me a lot! Thanks! :D

    Reply
  24. 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

    Reply
    • Hey Jay,

      Were you able to work this out? I am still looking for a solution.

      Reply
  25. 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?

    Reply
  26. 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 :)

    Reply
  27. Didn’t work. The query tags are still showing if I’m using W3TC. :(

    Reply
  28. 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!

    Reply

Trackbacks/Pingbacks

  1. Remove the version query strings from [WordPress] JavaScript JS and CSS Stylesheet Files | Clynt.com - […] Remove query strings from static resources […]

Submit a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Pin It on Pinterest

Share This