[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:

/wp-includes/js/jquery/jquery.js?ver=1.4.2

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:

		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 );

Hope you find this helpful!

John Chapman

Hello, I'm John Chapman. I am a SharePoint Developer for Sitrion (formerly NewsGator) living in Denver, Colorado. I develop solutions using SharePoint and .NET, and I thrive on the challenge of writing code to overcome the impossible, annoying, or otherwise difficult obstacles.

More Posts - Website - Twitter - LinkedIn - Google Plus

  • thompson

    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.

  • thompson

    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];
    }

  • thompson

    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);

    • http://www.urbanorleans.com Dyce

      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.

  • http://blog.lokamaya.net Zen

    Nice shortcut. Thanks.

  • Daniel Freij

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

  • Nurul Imam

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

  • http://tomaserlich.cz/ Carl114

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

    • http://www.sharepointjohn.com John Chapman

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

  • http://www.extramina.in MovieBug

    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

  • Moha

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

  • Peter

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

  • Cerebralunit

    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?

  • http://www.1admin.ir masoud

    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 ?

  • http://www.submitinme.com/ Aravind

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

    Thanks

  • http://fauzi.web.id Alang Fauzi Bertuah

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

  • http://www.mylaptopsonly.com Computer Repair Redding

    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.

  • VISHAL

    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.

    • http://www.india365.org vamsi

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

      @Vishal, Did u fix the problem?

  • http://www.catterycelizes.nl kletskater

    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

  • vipin

    thanks alot for this code… :)

  • http://blogdum.com Blogdum

    Great code man..thanks

  • charlieC

    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.

  • http://webs4u.us/ JNPerez

    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 :)

    • http://www.sutanaryan.com/ Ryan S

      Or you can simply use WordPress built-in function

      remove_query_arg( ‘ver’, $url );

  • http://www.FirePips.com Victor @Forex Signals

    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?

  • Dylan Kuhn

    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.

    • Nick Wild

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

  • Allan

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

  • Jimmy Franko

    Thank John, your solution has worked really well. Thank a lot. My website is: http://theonlinecasinowebsites.com

  • http://www.jehzlau-concepts.com/ jehzlau

    Wow! This helped me a lot! Thanks! :D

  • http://www.jaydorsey.com/ Jay Dorsey

    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

  • Steven Adams

    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?

  • Pingback: Remove the version query strings from [WordPress] JavaScript JS and CSS Stylesheet Files | Clynt.com

  • http://www.pawpetdirectory.com/ Paw Pet Directory

    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 :)