How to remove Query Strings from JavaScript and CSS in WordPress

WP Super Cache vs W3 Total Cache: what is the best WordPress cache plugin?
Custom Post Types: How to create custom blog types in WordPress

Query strings are nothing more than sets of pairs appended to the URL, basically, they are part of the URL. And within these URLs, there are parameters such as the ” ? “And” & “. These parameters are for developers to add functionality within a given site.

Static features such as JavaScript and CSS, are usually cached by proxies or CDNs . When a developer makes a change, it will not be processed instantly due to storage purposes into cache, which is when the query strings come into play.

These queries are not cached, allowing updates to be automatically rendered. However, it will also increase the load time of a page. The website optimization tools suggest ” remove query strings from static resources “, that is, remove the query strings from static resources to increase site speed.

This is very useful, especially for a WordPress site, as will emerge many benefits. Remove query strings from static resources” will enable the caching proxy servers, increasing the overall speed of WordPress website. Your CSS and JavaScript will make fewer server requests, reducing the use of your site’s resources. This will also give a “boost” to the SEO of your website because when optimization is crafted within a site, it will have a good position in Google ranking.

The downside of ” remove query strings from static resources ” on WordPress can be supplied by the management of your cache, which will be explained in detail below.

What will you need?

  • FTP access
  • Administrator access WordPress control panel

Step 1 – Analyzing the site

In this step, we recommend using GTmetrix . See the results below after reviewing WordPress site test.removing the query strings

Depending on your theme, site size and other factors, the amount of CSS and JavaScript files can vary. Every time a user visits your site, the browser makes a request for CSS, JavaScript or other static content. This will generate many requests to your server and your site load time will be longer. If you are using a CDN , browser cache or cache WordPress plugins, your files can not be cached, resulting in a slower delivery of files. Removing query strings, the cache WordPress site will improve with the reduction of resource use.

Step 2 – Removing query strings in WordPress

There are a few ways to do this.

Option 1 – Removing query string using plugins

The most convenient way to remove Query Strings of your website is by using the plugin  Speed BoosterAfter download it, go to the options it as shown in the image below:options in the plugin speed booster pack to remove the query strings wordpress

Now, select the “box Remove query strings” and then save the changes.selecting the option to remove query strings in the plugin

You can also use plugins like ” Remove query strings from static resourcesor ” Query remove strings ” to achieve the same goal.

Option 2 – Remove query strings from static resources in WordPress using funções.php

Important: Make sure you have a backup of funções.php file before making any changes.

Query strings can be removed from WordPress by adding the following code at the bottom of funções.php file:

// 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 );

In some cases, depending on the WordPress configuration, this code can cause the error 500 – Internal Server Error. If this happens, the plugin first step will help you.

Combating the negative side to remove the query strings WordPress

Managing the Cache WordPress correctly will allow you to provide the latest files to all visitors without causing any problem. If you are using a WordPress cache plugin you only need to clean the WordPress cache after you make the changes. The same procedure goes if you implemented the browser cache on your site, to clear the browser cache , you will be able to see the latest changes made. These little tricks will make you “counter” this downside that the removal of query strings can cause.

Step 3 – Testing changes

For comparison, here are the results of analysis by GTmetrix after we remove the query strings from static resources in WordPress.testing whether the removal work

Conclusion

In this short guide, you learned how to increase the performance of your WordPress site removing query strings from static resources, such as JavaScript or CSS.