skip to Main Content

I would like to force a style.css reload for users, as their browser cache might mess up the design. I am trying to work with a version to it (f.e. style.css?ver=2).

Is that the best way to do it? Eventually I might look into running this dynamic as shown on link. But right now I cannot even get this to work.

I am using Salient Theme, and they have an enqueue.php script which loads the .css files if I am correct. For now, I have added the ?ver=2 to the code below. However, this still now changes the output when I look at my homepage. I have cleared cached and double checked whether query-strings are allowed. But server caching and WP-rocket seem to allow this. Any pointers on how I can get this working to force the style.css reload?

if( is_child_theme() ) {
    $nectar_theme_version = nectar_get_theme_version();
    wp_register_style( 'salient-child-style', get_stylesheet_directory_uri() . '/style.css?ver=2', '', $nectar_theme_version );
    wp_enqueue_style( 'salient-child-style' );
}

UDPATE:
I have tried changing that code to /style2.css. That does seem to update the output. So seems I am in the right settings, but the query is being cut off.

3

Answers


  1. Put these meta tags in your header.php file (inside head tag):

    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="-1">
    <meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
    
    Login or Signup to reply.
  2. I would programmatically version it using filemtime. Even if the version is not showing, the browser should be reading the new file:

    if( is_child_theme() ) {
            wp_enqueue_style( 
                'salient-child-style', 
                get_stylesheet_directory_uri() . '/style.css', 
                [], 
                filemtime( get_stylesheet_directory() . '/style.css' )
            );
        }
    

    Basically, this will get the file’s modified time and if it’s different, the version will update and let the browser know the version changed. Also you really don’t need a separate wp_register_style, but if you want to keep it:

    if( is_child_theme() ) {
            $nectar_theme_version = nectar_get_theme_version();
            wp_register_style( 'salient-child-style', get_stylesheet_directory_uri() . '/style.css', '', filemtime( get_stylesheet_directory() . '/style.css' ) );
            wp_enqueue_style( 'salient-child-style' );
        }
    
    Login or Signup to reply.
  3. Best way is to give version to your styles after each update!

    style.css?ver=1.0
    
    style.css?ver=2.0
    
    style.css?ver=3.0
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search