3

Any expert who knows about removing Remove Query Strings From Static Resources plugin? It's not working.

Also, I have tried to write this code in function.php file of my theme

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

This is also not working for me.

I have also unchecked "Prevent caching of objects after settings change after settings change" in W3 Total Cache, but I'm still getting the same result in GTMetrix.

Can anybody help me? I am using the Twenty Twelve theme. I have also tried the same techniques in other themes.

Below is a screenshot of the message form GTMetrix: screen shot of problem

3 Answers 3

5

It looks like you are using JetPack's Photon which adds query strings to your URLs. According to this thread there is no way to remove them https://wordpress.org/support/topic/how-to-remove-photon-query-string?replies=2

If you want to get rid of the query strings I would suggest deactivating photon, using a CDN which does not add query strings, and using the snippet you mentioned:

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

It is a bad idea to remove query string as it is the best way to bust caches.

Hmmm, maybe I should rephrase it. The cost of implementing a url scheme which will "hide" the parameters while still letting you bust the cache is too much for simple wordpress sites to implement. In theory you can do it, but in practice I never saw anyone do it.

0

As mentioned by Mark Kaplun, sometimes query strings are necessary for a website. In this instance, having the ?w= query sets the image size.

If you still want to remove those queries, use the following code in your functions.php:

add_filter( 'script_loader_src', 'wpse_216912_remove_query' );
add_filter( 'style_loader_src', 'wpse_216912_remove_query' );

function wpse_216912_remove_query( $src ) { // Remove query strings from static resources also jetpack query
   if ( strpos( $src, '?ver=' ) || strpos( $src, '&ver=' ) || strpos( $src, '?w=') || strpos( $src, '?a600e5')  ) {
        $src = remove_query_arg( array( 'ver', 'w', 'a600e5' ), $src );
    }
    return $src;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.