Leverage browser caching not working after updating .htaccess

This should do it

if ( !is_admin() || !is_admin_bar_showing() ){
  add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
  add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
}

function _remove_script_version( $src ){
  if (preg_match("(\?ver=)", $src )){
    $parts = explode( '?', $src );
    return $parts[0];
  }else{
    return $src;
  }
}

This will remove the ?ver= from the file url. There’s a conditional check to see if we are on an admin page or if we are logged in. In that case, we don’t apply the filters (might be useful information when you work on your site).

Also note that this won’t remove ALL query strings since some plugins/themes might add their own and would use other filters or hardcode it within their code base. But you could hook that same function to those filters if they are provided (if not, you would need to edit those files manually)

For instance, you can add add_filter( 'genesis-header', '_remove_script_version', 15, 1); to that filter list if you were using a genesis theme.

Leave a Comment