Remove Query String from WordPress CSS and JS files


You need to remove query string from WordPress CSS and JS files if you want them to be cached. There is a simple function to do this that I’ll share with you.

Some plugin developers add their own versions at the end of their CSS and JS files and if you see the source of the page you will notice something like this:

<link rel=’stylesheet’ id=’itempropwp-css’ href=’https://www.vionblog.com/wp-content/plugins/itempropwp/assets/css/itempropwp.css?ver=3.4.0‘ type=’text/css’ media=’all’ />

CSS with ver string

This is useful for the developer and it’s used when updating plugins, the CSS and JS files will not be cached and even if they are after the update the developer will add new version number and all the changes will be applied right after the update.

But the performance problem with query strings added like this is that some browsers and proxy servers don’t cache them, even some CDN’s also don’t cache files with query string.

To solve this problem you need to edit your themes functions.php file and add the following code:

vi wp-content/themes/<your_theme>/functions.php
// Remove ?ver=x.x from css and js
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 );

After adding the code you should get your CSS and JS files without the ?ver=3.4.0 part like this:

<link rel=’stylesheet’ id=’itempropwp-css’  href=’https://www.vionblog.com/wp-content/plugins/itempropwp/assets/css/itempropwp.css’ type=’text/css’ media=’all’ />

CSS without ver string

This way all your CSS and JS files will be cached which will result in reduced bandwidth and server overhead.

NOTE: IF you are using proxy caching, when updating plugins because the version is gone and if there are changes in the CSS and JS files you will need to clear your proxy cache or your website may look funny.



Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Vincy
Vincy
10 years ago

Just pass null for argument version in wp_enqueue_style.

Karim
Karim
7 years ago

Please try our free plugin, it’s very fast loading because no database query:

https://wordpress.org/plugins/remove-query-strings-littlebizzy/

And can custom by defined constant too! ;) thanks bro

Advertisement