How to remove version and protocol from wordpress CSS and JS includes

By default, all WordPress CSS and Javascript files are included something like this:

<link rel="stylesheet" href="https://webtoolsplus.com/wp-includes/css/dashicons.min.css?ver=4.3.1" />

I personally don’t like the use of full file URL and version when including CSS or JS, because query string (the ?ver= part) sometimes messes with caching files in browser and/or proxy servers, and the protocol (http/https) can be bothersome when migrating to https or if your site is accessible in both versions.

So, I wrote a simple function that removes all this unnecessary bloat from the href attribute and produces simpler, cleaner href value which looks like this:

<link rel="stylesheet" href="/wp-includes/css/dashicons.min.css" />

The code is really simple. Just add the following filters to your themes functions.php file:

/**
* Remove version, protocol and domain name
* from script and stylesheet urls, by webtoolsplus.com
*/
add_filter('script_loader_src', 'clean_src');
add_filter('style_loader_src', 'clean_src');

function clean_src($src) {

    //remove protocol
    $src = str_replace(array('https://', 'http://'), '//', $src);

    //remove domain name
    $src = str_replace('//'.$_SERVER['SERVER_NAME'].'/', '/', $src);

    //remove ?ver= part
    $parts = explode('?ver=', $src);
    if(count($parts) === 2) {
        $src = $parts[0];
    }

    return $src;
}

You can comment out or remove the protocol and domain name part changing lines, if you only want to remove version, but I see no practical reason to have them in my urls.

One thought on “How to remove version and protocol from wordpress CSS and JS includes

  1. Nures September 30, 2015 / 10:07 am

    I think this would be a better approach for removing parameter from the query string, because it won’t remove other, more useful parameters, in case there are any.

    $src = remove_query_arg( 'ver', $src );

Leave a Reply to Nures Cancel reply

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