Wordpress - How to remove the WordPress version from some .css/.js files
You can check for the current handle before removing the version.
Here's an example (untested):
add_filter( 'style_loader_src', 'sdt_remove_ver_css_js', 9999, 2 );
add_filter( 'script_loader_src', 'sdt_remove_ver_css_js', 9999, 2 );
function sdt_remove_ver_css_js( $src, $handle )
{
$handles_with_version = [ 'style' ]; // <-- Adjust to your needs!
if ( strpos( $src, 'ver=' ) && ! in_array( $handle, $handles_with_version, true ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
Since this question was written, a lot has changed with WordPress. Here is a new way to prevent WordPress from displaying its version number:
// remove version from head
remove_action('wp_head', 'wp_generator');
// remove version from rss
add_filter('the_generator', '__return_empty_string');
// remove version from scripts and styles
function remove_version_scripts_styles($src) {
if (strpos($src, 'ver=')) {
$src = remove_query_arg('ver', $src);
}
return $src;
}
add_filter('style_loader_src', 'remove_version_scripts_styles', 9999);
add_filter('script_loader_src', 'remove_version_scripts_styles', 9999);
No editing required. Add to functions.php and done. Or add via simple custom plugin. The choice is yours! :)
Original article