DEV Community

Cover image for Remove version strings from CSS and JS link hrefs in WordPress
Chris Texe
Chris Texe

Posted on

2 1

Remove version strings from CSS and JS link hrefs in WordPress

Yesterday I gave you a quick tip how to remove meta generator tag in WordPress. After applying this tip you can still see the WordPress version in the source code:

WordPress version in link hrefs

What to do if you want to hide it? Just apply below code into functions.php file of your theme.

//Remove version strings from CSS and JS link hrefs
function ct_remove_version_strings( $src ) {
global $wp_version;
parse_str(parse_url($src, PHP_URL_QUERY), $query);
if ( !empty($query['ver']) && $query['ver'] === $wp_version ) {
$src = remove_query_arg('ver', $src);
}
return $src;
}
add_filter( 'script_loader_src', 'ct_remove_version_strings' );
add_filter( 'style_loader_src', 'ct_remove_version_strings' );
view raw functions.php hosted with ❤ by GitHub

If you want more tips follow me on Twitter: @TexeChris:

Top comments (1)

Collapse
 
moopet profile image
Ben Sinclair

This is probably there to ensure people don't hang on to outdated versions in their caches. You can set expires and so on but cache invalidation is still one of the hard problems and it doesn't always work how you want. If they added this to Wordpress, it's probably because there were issues raised with it not working.

You can get the same effect but without giving away version information by doing what you're doing but then adding another query string item like ?cachebust=86452542 where that number is linked to the version number in a non-reversible way like this (untested, I don't have Wordpress):

$src = remove_query_arg('ver', $src);
$src = add_query_arg('cachebust', md5($wp_version . "salt here"), $src);
Enter fullscreen mode Exit fullscreen mode
👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay