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. :)

2 thoughts on “Remove Query String on CSS & JS Files

  1. Niche MasterNiche Master

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

    [code lang=”php”]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 );[/code]

    Reply
    1. PothiPothi

      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…

      [code]subs_filter_types text/html;
      subs_filter ‘\?ver=[0-9.]+’ ” gir;[/code]

      Reply

Leave a Reply

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