Remove Query Strings from Static Resources

Remove Query Strings from Static Resources

You might have come across this page after testing your website on Pingdom, GTmatrix, Google Page Speed Insights or YSlow OR when viewing your source code you see URL’s that look like this:

http://mydomian.com/wp-content/plugins/js/some_js?ver=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.

In this tutorial, I’ll discuss, What are the Query Strings in WordPress code structure? Why you should Remove Query Strings from Static Resources? How you can Remove Query Strings and Increase your WordPress Website Speed? And Removing Query Strings from Static Resources will increase your web page speed or not? So let’s begin…

Page load time is an extremely essential aspect for a website or blog. It help you to deliver a satisfactory and effective user experience to your visitors. It also help you to get higher ranking in Google Search which results increase in overall traffic on your website. As Google already announced that page load time affect your search engine ranking. Caching of Static Resources like CSS and JavaScript plays an important role in page loading time. Query Strings prevents Caching of Static Resources on Proxy servers and on browsers. By Removing Query Strings from Static Resources you can enable caching of static resources and can achieve a significant improvement in page load speed with less page load time.

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 do we remove those query strings?
You will need to locate your themes functions.php files, copy and paste this code inside your functions.php

function remove_cssjs_ver( $src ) {
if( strpos( $src, '?ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );

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

Removing Query String from Static Resources like CSS & JavaScript is important, if you want to enable Caching of Static Resources on Proxy Servers and want to increase your website page loading speed.

Walid BaniHani

 

College of Applied Sciences - Al Rustaq

2 thoughts on “Remove Query Strings from Static Resources

  1. Hello. I do not have a WordPress site…I coded it myself and more or less taught myself how to code, however clumsily. I have tried adding it in multiple locations to no avail. I can’t get that code to work. When I enclose that code in php open and end tags and add it to my header.php file it breaks all of my content. Any advice would be greatly appreciated.

    P.S….You have the acronym in your last blog post as CND not CDN. 🙂

Leave a Reply

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