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-versionarrow-up-rightSharearrow-up-rightImprove this questionarrow-up-rightFollowasked Jul 29 '16 at 9:18arrow-up-rightMikhail Morfikovarrow-up-right30311 gold badge22 silver badges99 bronze badgesAdd a commentarrow-up-right

3 Answers

Activearrow-up-rightOldestarrow-up-rightVotesarrow-up-right10

You can check for the current handle before removing the version.

Here's an example (untested):

Sharearrow-up-rightImprove this answerarrow-up-rightFollowanswered Jul 29 '16 at 9:48arrow-up-rightbirgirearrow-up-right60.5k77 gold badges8787 silver badges202202 bronze badgesAdd a commentarrow-up-right5

Since this question was written, a lot has changed with WordPress. Here is a new way to prevent WordPress from displaying its version number:

No editing required. Add to functions.php and done. Or add via simple custom plugin. The choice is yours! :)

Original articlearrow-up-rightSharearrow-up-rightImprove this answerarrow-up-rightFollowedited Mar 14 '19 at 18:15arrow-up-rightanswered Dec 26 '18 at 17:31arrow-up-rightAmrarrow-up-right15911 silver badge55 bronze badges

Add a commentarrow-up-right0

To target a specific file

Last updated