Remove WordPress version from JS and CSS files

Spread the love

I recently faced an issue while working on SEO of my blog. I figured that version numbers appended to js and css files is one of them, so thought of finding a way to remove wordpress version from js and css files.

To remove WordPress version from js and css files, we can use one of the following 2 methods.

Add the code for one of the below methods in your functions.php file placed at following path: /wp-includes/functions.php

The first method removes the “ver” parameter from all the enqueued CSS and JS files and the 2nd method removes the “ver” parameter only if its value matches the current WordPress version number of your website.

Method 1:

function krafi_css_js_ver_remove( $src ) {
    if ( strpos( $src, 'ver=' ) )
        $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'krafi_css_js_ver_remove', 9999 );
add_filter( 'script_loader_src', 'krafi_css_js_ver_remove', 9999 );

Method 2:

function krafi_css_js_ver_remove( $src ) {
    if ( strpos( $src, 'ver=' . get_bloginfo( 'version' ) ) )
        $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'krafi_css_js_ver_remove', 9999 );
add_filter( 'script_loader_src', 'krafi_css_js_ver_remove', 9999 );

Read my other post: How to load WordPress posts using WP_Query