Remove query strings from CSS and Javascript

Theme and plugin developers often add a version number to the CSS and Javascript files. This way they force the browser to always load the most recent versions. It also causes these files not to be cached, because of the query string added to the file name. So every file must be reloaded every time it is needed. This affects the performance of your website.

You can remove the query strings by using a plugin. A caching plugin will also remove the query strings. If you don’t want to use a plugin, you can use the code below.

// Remove query string from static files
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 );

Put the code into the functions.php of your theme. Test the effect with a tool like GTmetrix. Run the test before and after you put the code into functions.php and see the difference under “Remove query strings from static resources”.

Always backup the functions.php before you start changing it!

0 0 votes
Article Rating
Notify of
newest most voted
Inline Feedbacks
View all comments
4 years ago

Really helpful. Results with GTMetrix or Pingdom tools show a real difference, and I use this snippet for years on each blog I create.

But I rather like to use an extension such as “Code Snippets” for my own snippets rather than putting them in the functions.php file

3 years ago

Hmm but why don’t use an easy plugin guys?

So if change theme, no problem…

Would love your thoughts, please comment.x