0

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)

5
  • Your enqueue of style.css should be found in functions.php - Note that date will make it so that it is never cached. But you could set the versioning to the last date the file was modified Commented May 8, 2019 at 8:50
  • How I can do that? "t you could set the versioning to the last date the file was modified" Commented May 8, 2019 at 8:55
  • you could use filemtime( get_stylesheet_directory() . '/style.css' ) as the last parameter of your enqueue Commented May 8, 2019 at 8:58
  • this is best in a child theme though, since you will loose this on updates if you do it in a parent theme Commented May 8, 2019 at 9:00
  • @Stender your code didn't worked for me. Commented May 8, 2019 at 12:05

1 Answer 1

1

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*/

Sign up to request clarification or add additional context in comments.

2 Comments

Not working for me, sorry. It keeping without updating
Can you try just doing the dequeue action without re-enqueueing it? To see if that works.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.