If you want to improve your WordPress site speed and SEO, one simple optimization is to remove query strings from CSS and JS files. By default, WordPress adds a ?ver= query string to static resources like CSS and JavaScript files. This can prevent caching on proxy servers and CDNs, negatively affecting your site’s SEO performance.
In this guide, we will show you how to remove query strings from your WordPress static files using a simple snippet added to your theme’s functions.php file.
Why Remove Query Strings from CSS and JS?
Query strings like ?ver=5.9 make it harder for some caching systems to store static files efficiently. Removing them can:
- Improve page load speed
- Enhance caching for CDNs and proxy servers
- Boost your SEO performance
For more insights on WordPress performance optimization, check out Google PageSpeed Insights.
How to Remove Query Strings from CSS and JS
Follow these steps:
- Open your WordPress theme folder and locate the
functions.phpfile. - Add the following code at the end of the file:
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 );
This code will remove the version query string from all your CSS and JS files.
Conclusion
Removing query strings from CSS and JS in WordPress is a small tweak that can significantly improve caching, page speed, and SEO. Add the code snippet above, test your site, and pair it with a caching plugin for the best results.
If you found this guide helpful, please share it with your fellow WordPress developers.