Disable wordpress caching of styles using “ver” url parameter

After every link that is enqueued in php using wp_enqueue_style, wordpress automatically inserts ?ver= and the wordpress version number. For example .../style.css?ver=4.4.2

In chrome this makes it almost impossible sometimes to refresh the file.

Related posts

2 comments

  1. I solved this one by adding a filter on the style loader hook that replaces the version number with the current time value. Works a treat, but not to be used in production!

    // TODO Remove this once put in a production environment
    add_filter('style_loader_tag', 'development_disable_style_caching');
    function development_disable_style_caching($tag){
      return str_replace(get_bloginfo('version'), time(), $tag);
    }
    
  2. If you want to remove :
    Add the below to your functions.php file

    // Remove WP Version From Styles    
    add_filter( 'style_loader_src', 'sdt_remove_ver_css_js', 9999 );
    // Remove WP Version From Scripts
    add_filter( 'script_loader_src', 'sdt_remove_ver_css_js', 9999 );
    
    // Function to remove version numbers
    function sdt_remove_ver_css_js( $src ) {
        if ( strpos( $src, 'ver=' ) )
            $src = remove_query_arg( 'ver', $src );
        return $src;
    }
    

    For Custom Version
    Version number is a parameter of wp_enqueue_style().

    Per the Codex, here are all the parameters that wp_enqueue_style accepts.

    wp_enqueue_style( $handle, $src, $deps, $ver, $media );
    

    So for example to load a stylesheet with a version number you’d do the following:

    function wpa_90820() {
        wp_enqueue_style('my-styles', get_stylesheet_directory_uri() .'/my-styles.css', '', '1.0' );   
    
    }    
    
    add_action('wp_enqueue_scripts', 'wpa_90820'); 
    

Comments are closed.