adit's avatar

Aditya Firmansyah

Just a simple Blog

Remove Query Strings From Static Resources

I believe you come here after scanning your wordpress blog with awesome tools from gtmetrix.com, but if it's not, let's just assume it's right, ehe.. ok so, after you scan your wordpress blog you got notification from gtmetrix that you need to remove query strings from static resources easy to say but hey.. wordpress generated those css and javascript inside wp_head() function, its not as easy as change html source code. Then how to do that..?

Many folks out there suggest to use minify plugin like w3c total cache that generate minify version of javascript and css, but what if your hosting cant handle that feature..? I've been working with many clients with bunches of javascript and css inside their wordpress that causing 500 Server Error when using minify plugin, if that happen use free service like Cloudflare will help to do minify job. Hey Adit! just tell me how to remove those annoying query strings. Huft, okay then..

There are two methods to remove query strings from wordpress:

1. Use plugin “Remove Query Strings From Static Resources”

Do I need to explain this..? ok, you just need to install “Remove Query Strings From Static Resources” to your wordpress blog then activate it. That is simple, isn't it?

2. Adding custom function inside functions.php inside your theme

Ok fellow, if you chose to use this method, you are really my friend. :)
This function is actually same as that plugin, but I know you will use this method my friend. ok no need much further delay, here is the code:

function remove_query_strings( $src ){
    if ( strpos( $src, 'ver=' ) ) $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'script_loader_src', 'remove_query_strings', 15, 1 );
add_filter( 'style_loader_src', 'remove_query_strings', 15, 1 );

I know you do not need me to tell you how to insert this code inside functions.php so I'll explain what the actually code do.

function remove_query_strings( $src ){
    if ( strpos( $src, 'ver=' ) ) $src = remove_query_arg( 'ver', $src );
    return $src;
}

This code will find query string “ver=” then passed to remove_query_arg() function to remove it from $src variable then return it.

add_filter( 'script_loader_src', 'remove_query_strings', 15, 1 );
add_filter( 'style_loader_src', 'remove_query_strings', 15, 1 );

This code is adding filter to return the javascript and jss location without query strings. for further information about each of those function you can find it here:
http://id1.php.net/strpos
http://codex.wordpress.org/Function_Reference/remove_query_arg
http://codex.wordpress.org/Function_Reference/add_filter