Pedro Corchero Murga
Pedro Corchero Murga

Reputation: 505

Problem with my Wordpress CSS version Cache updating

I have a blog on Wordpress. I usually update my CSS file, the CSS file have a tag like:

<link rel='stylesheet' id='rit-style-css'  href='https://www.lagaleramagazine.es/wp-content/themes/ri-maxazine/ri-maxazine/style.css?ver=5.2' type='text/css' media='all' />

You can see live here: https://www.lagaleramagazine.es

The problem is that the browser don't update my file, it's keeping the same version (5.2), so the changes that I did to the file didn't show.

I tried to search the CSS file on .php file on my theme but I can't get it to work.

If you visit the file with https://www.lagaleramagazine.es/wp-content/themes/ri-maxazine/ri-maxazine/style.css?ver=5.3 you will see a differente version that https://www.lagaleramagazine.es/wp-content/themes/ri-maxazine/ri-maxazine/style.css?ver=5.2.

I searched a solution but the problem is I can't find the CSS line tag on my Wordpress php files:

/css/style.css?v=<?php echo date('his'); ?>

(I don't know it this will work)

Upvotes: 0

Views: 2734

Answers (1)

Sephsekla
Sephsekla

Reputation: 441

The best way I've found of forcing a stylesheet to enqueue the latest version is using the filemtime() function, which will use the modified date of the file as the latest version. That way it will be relatively cache friendly but also update when the style is.

e.g.


    wp_enqueue_style( 'rit-style-css', get_stylesheet_directory_uri() . '/style.css', array(), filemtime(  get_stylesheet_directory() . '/style.css' ) );

Make sure to put it in a function that's added to the wp_enqueue_scripts action if it's not already.

If you're working in a child theme or similar then you'll need to dequeue the file before enqueuing it again.


function example_update_stylesheet(){

       wp_dequeue_style( 'rit-style-css' );

       wp_enqueue_style( 'rit-style-css', get_stylesheet_directory_uri() . '/style.css', array(), filemtime(  get_stylesheet_directory() . '/style.css' ) );


}

add_action( 'wp_enqueue_scripts', 'example_update_stylesheet', 100 ); /*Late priority*/

Upvotes: 1

Related Questions