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

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

Similar Posts

25 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

    1. No men, give it a try it’ll not hurt your website speed and by the way. Which caching plugin are you using so I can tell you in details about whether you should try or not.

  2. Is this method still applicable if Im already using seo plugin by Yoast? Thanks Muhammad. And by the way, your site is great!

    1. Hey @Mark, thanks for admiring my site. I’m pleased to read your precious comment. And by the way, this method is still acceptable to well known web browsers…

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

  4. I tried this script and I get A91 there is still some line to remove ! and I don’t know how to complete it.

  5. 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.’

  6. 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 *