WordPress Optimization | Remove Query strings from Static Resources

Query strings – What are they and why should we remove them?

Do something right now. Right click on your site and view source.

What do you see?

I bet when viewing your source code you see URL’s that look like this:

http://sitename.com/wp-content/plugins/js/some_js?ver=3.4.2

or something to that effect.

They can be seen on images, CSS and JavaScript in most cases. Most plugins affix them to their URL’s but there is a problem with them.

Firstly – it can give away your WordPress version. In most cases this isn’t a problem as you should be keeping up to date as WordPress releases regular security fixes. But if your running an older version of WordPress some people may pick up on that and exploit the vulnerabilities which will be widely known by that point.

Secondly – Most are not cached by some proxy caching servers. These include content delivery networks.

Removing these queries from static resources such as CSS and JavaScript is simpler than you think, as you know I hunt the web for solutions to your problems, so that you may benefit from the results. A faster web benefits everyone.

So how to do we remove those query strings?

You will need to locate your themes functions.php files, for thesis users, you will edit custom_functions.php

[note]caution when editing your functions.php or custom_functions.php for thesis, I recommend editing using your file manager on your server in case you do something wrong, you can quickly reverse it.[/note]

copy and paste this code inside your functions.php

Update: 04/05/2013 – please remember to add the code inside the PHP tags of your functions.php file.

function _remove_script_version( $src ){
	$parts = explode( '?ver', $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 your file then clear cache. Refresh your site with control f5 to clear your browser cache.

Again go back and view your source, there should be no more query strings on your CSS and JavaScript URL’s

So in just 5 minutes or less, you have just improved your site.

I hope you found this useful and don’t forget to sign up for more great ways to improve your site.

Meet the Author

Matthew Horne

Matthew Horne is web developer who specializes in optimized development. He also builds custom solutions instead of reverting to plugins. Matthew Has a strong understanding of PHP, JavaScript, jQuery.

77 comments… add one

  • Christene Feb 20, 2015, 7:43 pm

    Ah, I had “Prevent caching of objects after settings change” enabled. I disabled it, and all appears to be good now. Thanks.

    • Matthew Horne Feb 24, 2015, 4:18 am

      Yes that would add version control to your resources via W3TC i believe.

Leave a Comment