9

I observed the inside the wp_head function in the source links of every .css, .js files a ?ver=1 (or other number based on the file's/library version) is added. How can I overwrite them, to remove them?

This issue I think is causing problems on the cache manifest part.

2 Answers 2

15

You can hook into style_loader_src and script_loader_src and run remove_query_arg( 'ver', $url ) on the URL:

<?php
/* Plugin Name: Remove version parameter for scripts and styles */

add_filter( 'style_loader_src', 't5_remove_version' );
add_filter( 'script_loader_src', 't5_remove_version' );

function t5_remove_version( $url )
{
    return remove_query_arg( 'ver', $url );
}

Without this plugin:

enter image description here

After plugin activation:

enter image description here

There is one case where that will fail: When someone didn’t use the script/style API, but added a hard coded string to the header.

2
  • Neat use of remove_query_arg(). Apr 27, 2017 at 19:29
  • I prefers to hide versions, but I'm curious if there are any side effects from using it?
    – kanlukasz
    Sep 1, 2022 at 14:24
1

This worked for me when I still had to load a stylesheet from Google Fonts.

<?php
add_filter( 'script_loader_src', 'wpse130419_remove_script_version', 15, 1 );
add_filter( 'style_loader_src',  'wpse130419_remove_script_version', 15, 1 );
function wpse130419_remove_script_version( $src ) {

    $url = explode( '?', $src );

    if ( $url[0] === 'http://fonts.googleapis.com/css' ) :
        $version = explode( '&ver=', $url[1] );
        $url[1]  = $version[0];
    endif;

    return ( $url[0] === 'http://fonts.googleapis.com/css' ) 
        ? "{$url[0]}?{$url[1]}"
        : $url[0]
    ;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.