How to: Remove Query String URL From Static Resources in WordPress?

(Last Updated On: April 30, 2013)

We have experienced in WordPress that it generates automatically query string URL by adding a “?” question mark inside of a URL. Which is somewhat not cacheable by most proxies and notably squid up through version 3.0 even if a cache-control:public header is present in response. What actually I mean is that in order to speed up a website we need it to be cleaned up from untidy query string URLs… Let me tell you a little bit more about those files which are being caused with query string URL. Mostly these files are CSS or JS…

Removing Query string URL from WordPress

Here are some examples about query string URLs:

http://1.gravatar.com/avatar/da2d44be2b1df905b5e297f7ee754c4f?s=65&d=&r=G
https://softstribe.com/wp-content/plugins/wp-table-reloaded/css/datatables.css?ver=1.9.4
https://softstribe.com/wp-content/plugins/wp-table-reloaded/css/plugin.css?ver=1.9.4

You can see at the end of each URL mentioned above has a question mark and now the next parameters after “?” in the URL will be termed as query string URL.

How to Remove Query String URLs?

In this article I’m going to show you how to remove query string “?” from static resources especially. What we are going are to do is actually adding simple line of codes inside of our functions.php file before the closing php tag ?>.

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

Save the file and try testing it again… Hope you will find your website cleaned by 100%.

22 comments

  1. Gday,

    I have stumbled across this site in the last moments before giving in for the night and you saved me! Thankyou! I put a fresh jug on and will continue into the early hrs now.:)

    One huge problem I have is that whenever I try to apply that fix for the query strings the clients whole site comes tumbling down. Luckily I have been burnt enough that I made a copy of the original first.
    Any suggestions? It is a particularly problematic wordpress theme kallyus, but I am so close to getting it to a mid 90’s score that I just won’t be satisfied until I rule out every possibility.

    Anyway, consider me a regular here from now on,

    andY

  2. Doesn’t work for me. Everytime I add it to my functions.php code it either breaks the site or completely alters the fonts on the page. I’m using Genesis Epik theme.

  3. The same is happening where it is not removing the query strings completely, what’s happening is that after the javascript query it is adding a period.

    ex. ‘yith-woocommerce-order-tracking/assets/js/ywot.js.’

  4. Hi
    If you are using worpress multisite, it will remove query strings on the main site but not the other sites even if you post it in every theme functions.php.

    For me only worked on site 1 out of three sites.

Leave a Reply

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

five × one =