Remove Query Strings in WordPress CSS styles and JS Scripts

Remove Query Strings in WordPress CSS styles and JS Scripts

When you’re optimizing your website, you’ve probably noticed a page speed score telling you to remove query strings from your CSS and JavaScript files in WordPress. Query strings are URLs that have either ? or &.

But why do these appear in the URLs and what’s the cause? Actually query strings are used by plugin developers to push updates.

The static resources (like CSS and JS files) are usually cached by proxy servers or CDNs. And when a developer has to push an update, it won’t be rendered immediately because of the caching effects. Which is why they use query strings to instantly render updates, as query strings won’t be cached. But as a side effect, since they are not cached, they contribute to the loading time of a web page.

Code Snippet

Place this code snippet in your functions.php file. It will remove all version numbers that are appended to the end of enqueued CSS and JavaScript files in your WordPress theme. Simply, turn this http://yoursite.com/style.css?ver=1.0 into this http://yoursite.com/style.css

/**
Remove Query strings from css styles and js scripts
*/

function remove_q_strings($src, $handle){
   $src = remove_query_arg('ver', $src);
   return $src;
}
add_filter( 'script_loader_src', 'remove_q_strings', 15, 2 ); //Where $priority is 15, $accepted_args is 2.
add_filter( 'style_loader_src', 'remove_q_strings', 15, 2);   //Where $priority is 15, $accepted_args is 2.

When to remove version numbers from WordPress files

During website optimization, determine if your CSS and JavaScript files will change often. If they’ll stay fairly static, you’re safe to remove the version number.

This helps with page speed scores from Pingdom , Google Analytics and Google Page Speed . And it really not needed until you make changes again.

Optimization is an important step after you’ve launched your new WordPress website. After development of all CSS and JS, there’s typically no need to include versioning of your CSS and JS files.

SUBSCRIBE FOR NEW ARTICLES

@
comments powered by Disqus