By default, all WordPress CSS and JavaScript files are included like this:
<link rel="stylesheet" href="https://webtoolsplus.com/wp-includes/css/dashicons.min.css?ver=4.3.1" />
I personally don’t like using the full file URL and version when including CSS or JS because the query string (the ?ver= part) sometimes interferes with caching in browsers and/or proxy servers. Additionally, the protocol (http/https) can cause issues when migrating to HTTPS or if your site is accessible via both versions.
So, I wrote a simple function that removes this unnecessary bloat from the href
attribute and produces a simpler, cleaner href
value that 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.
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.