How to Remove Query Strings from Static Resources in WordPress

If you’ve tested the speed of your site on a tool like Pingdom or GTmetrix you may have seen the message to remove query strings from your static resources.

Here’s our guide to removing query strings from static resources with and without a plugin:

How to Remove Query Strings from Static Resources with a Plugin

Most performance plugins include an option to remove query strings like W3 Total Cache and our personal favorite WP Rocket.

If you currently aren’t using a performance plugin with this option there are plenty of WordPress plugins designed to only remove query strings from static resources.

The easiest way to remove query strings from static resources is to install the “WP Remove Query Strings From Static Resources” plugin from WordPress.org.

This plugin will help improve scores in website speed tests like Pingdom/YSlow/PageSpeed/GTmetrix.

After you install and enable this plugin you can confirm the change by viewing the source of your homepage to confirm query strings are gone from css/js files.

How to Remove Query Strings from Static Resources without a Plugin

If you want to remove query strings from static resources with code this snippet below will do just that! You can use this in your theme’s functions.php or embed it with the Code Snippets plugin.

<?php
//Remove Query Strings From Static Resources
function smartwp_remove_query_strings_from_static_resources( $src ) {
if( strpos( $src, '?v=' ) ){
$src = remove_query_arg( 'v', $src );
}
if( strpos( $src, '?ver=' ) ){
$src = remove_query_arg( 'ver', $src );
}
return $src;
}
add_filter( 'script_loader_src', 'smartwp_remove_query_strings_from_static_resources', 999 );
add_filter( 'style_loader_src', 'smartwp_remove_query_strings_from_static_resources', 999 );
Andy Feliciotti

Andy Feliciotti

Andy has been a full time WordPress developer for over 10 years. Through his years of experience has built 100s of sites and learned plenty of tricks along the way.

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Tips Monthly

Get the latest from SmartWP to your inbox.