Remove Query String on CSS & JS Files

How to Remove Query String on CSS & JS FilesHave you seen WordPress version number as a query string after every CSS and JS file? You are not alone. It is easy to remove that string, even though there is little documentation available in the codex. Please note that it is different from what WP-Minify plugin adds in the minified version of the CSS and JS file. Having the query doesn’t allow certain browsers to cache the CSS and JS files. Once removed, it offers performance benefits on those browsers. Moreover, who would want to expose the version of WordPress at the end of these files?

Locate The File

To remove the string from your theme related files, fire up the theme editor and open the functions.php file or header.php file. It varies depending on the theme. If you’d want to remove the query string on CSS and JS files derived from any plugin, then please switch to the plugin editor and open the appropriate plugin file. Again it varies greatly depending on the plugin, but not so much as with the themes.

The version of WordPress is inserted by two functions…

Let’s Do It

In both functions, only the first parameter is required. The remaining four parameters are optional. The fourth parameter $ver sets the version number manually or automatically. If it is not defined, then the default value is false. Now to remove the query string from the URL, just change this value to null. That’s it. You’re done.

In some cases such as when you’d want to force CSS changes to “go live” immediately, having the query string in the URL is proven to be beneficial. Even in such a situation, we can setup automatic versioning of JS and CSS files.

Happy Coding. :)

4 Replies to “Remove Query String on CSS & JS Files”

  1. Or you could just stick this code into your functions file:

    function _remove_script_version( $src ){
        $parts = explode( '?', $src );
        return $parts[0];
    add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
    add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
    1. Thanks for the tip, Niche.

      Lately, I started doing this at the server level. For example, using Nginx’s HttpSubsModule, the following code removes the query string on certain files…

      subs_filter_types text/html;
      subs_filter '\?ver=[0-9.]+' '' gir;

Leave a Reply

Your email address will not be published. We use cookies to prevent spam comments. Required fields are marked *