Remove the WordPress version from some .css/.js files (ok)
https://wordpress.stackexchange.com/questions/233543/how-to-remove-the-wordpress-version-from-some-css-js-files
add_filter( 'style_loader_src', 'sdt_remove_ver_css_js', 9999 );
add_filter( 'script_loader_src', 'sdt_remove_ver_css_js', 9999 );
function sdt_remove_ver_css_js( $src ) {
if ( strpos( $src, 'ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'sdt_remove_ver_css_js', 9999 );
add_filter( 'script_loader_src', 'sdt_remove_ver_css_js', 9999 );
function sdt_remove_ver_css_js( $src ) {
if ( strpos( $src, 'ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
But I have some files, for instance style.css
, in the case of which I want to add a version in the following way:
function css_versioning() {
wp_enqueue_style( 'style',
get_stylesheet_directory_uri() . '/style.css' ,
false,
filemtime( get_stylesheet_directory() . '/style.css' ),
'all' );
}
But the previous function removes also this version. So the question is how to make the two work together?wordpress-versionShareImprove this questionFollowasked Jul 29 '16 at 9:18Mikhail Morfikov30311 gold badge22 silver badges99 bronze badgesAdd a comment
3 Answers
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;
}
ShareImprove this answerFollowanswered Jul 29 '16 at 9:48birgire60.5k77 gold badges8787 silver badges202202 bronze badgesAdd a comment5
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 articleShareImprove this answerFollowedited Mar 14 '19 at 18:15answered Dec 26 '18 at 17:31Amr15911 silver badge55 bronze badges
isn't this answer directly copied from here? You should always give credit to original author, unless you wrote that article – Vishwa Mar 13 '19 at 10:57
Added the link to the original article. No offense. I just wanted to help people solve the problem. I never took the credit for myself. – Amr Mar 14 '19 at 18:19
I didn't implied you did, just showed the proper way to do it. that's all :) – Vishwa Mar 15 '19 at 4:16
1Nice! Unfortunately it doesn't answer OP's question on how to keep it for one certain CSS. – leymannx May 28 '19 at 20:34
To target a specific file
// remove version from scripts and styles
function remove_version_scripts_styles($src) {
if (strpos($src, 'yourfile.js')) {
$src = remove_query_arg('ver', $src);
}
return $src;
}
add_filter('script_loader_src', 'remove_version_scripts_styles', 9999);
Last updated
Was this helpful?