1

I am using WordPress and I have style page link

https://example.com/blog/wp-content/themes/themename/style.css?ver=5.4.1

I have to change from style.css?ver=5.4.1 to style.css?v=<?=time();?>

Is it possible to change it?

Why I am doing this

I don't know How can I explain my issue with style. Why I am doing this because if I change anything in the style.css that is not updating on the browser. I mean, I added some css using theme editor and when I check my website then I am not getting my latest CSS. But If I go and check the theme editor then my latest code is there.

I tried everything, I haven't set any cache on my website. As of now it's totally empty. I contact GoDaddy for this issue they said we can't help in this contact your developer.

Now what I am doing is, My style.css file is totally empty and I create one more css file called main-style.css and I am adding all my css there. This file also the same issue. Then What I am doing. First I download this file then I add some css as per requirement and upload again then my css working.

6
  • @fuxia, I want to know why you change my tags from WordPress to css? This is not a css issue? May 26, 2020 at 17:56
  • It is your CSS file, right?
    – fuxia
    May 26, 2020 at 17:57
  • Yes the file is CSS but the issue is not CSS. May 26, 2020 at 17:59
  • What's the purpose? Are you trying to hide the WordPress version, or are you trying to keep your CSS file from being cached?
    – Pat J
    May 26, 2020 at 19:04
  • @PatJ, No I don't want to hide WordPress version. I want whenever I update my CSS that should reflect immediately after save. Let me explain my issue in detail in the question. May 26, 2020 at 19:08

2 Answers 2

1

When you enqueue the style, you can use filemtime() as the version:

<?php
add_action('wp_enqueue_scripts', 'wpse_367594_enqueue_version');
function wpse_367594_enqueue_version() {
    wp_enqueue_style(
        // Update this to your slug.
        'style-slug',
        // Update to your stylesheet URL if it's not the main style.css.
        get_stylesheet_directory_uri() . '/style.css',
        // Include dependencies here or pass an empty array.
        array('theme-dependency'),
        // Here's the line that sets the version.
        filemtime( get_stylesheet_directory() . '/style.css' ),
        'screen'
    );
}
?>

That should bust most layers of caching so visitors see the latest version whenever the file is updated.

6
  • I apologize for the late reply. I added the above code at the bottom of the function.php. Do I need to add anything more? Because it's not working. May 27, 2020 at 3:31
  • I added above answer inside themename_scripts function but still not working. May 27, 2020 at 4:01
  • Yes, you need a function that hooks it to the wp_enqueue_scripts hook. I've updated above with a complete example - but you'll still need to update some details such as the slug and dependencies for your particular stylesheet.
    – WebElaine
    May 27, 2020 at 13:07
  • Thanks for the answer, I added but it's still the same issue. instated of version can we show the time? May 27, 2020 at 14:23
  • This already does. It sounds like you may be either not customizing all the details, or possibly your host has some type of caching this doesn't bust. Have you turned on WP_DEBUG and checked error logs to see if anything is failing? You could also check with your host to see if they can help troubleshoot further.
    – WebElaine
    May 27, 2020 at 18:23
0

Finally, I got my solution, I don't know it's the best way or not but it's solved my issue.

What I did, I checked function.php file and found below code

function themename_scripts() {
    wp_enqueue_style( 'themename-style', get_stylesheet_uri(), array(), _S_VERSION );
    wp_style_add_data( 'themename-style', 'rtl', 'replace' );

    wp_enqueue_script( 'themename-navigation', get_template_directory_uri() . '/js/navigation.js', array(), _S_VERSION, true );

    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
}
add_action( 'wp_enqueue_scripts', 'themename_scripts' );

Then I remove _S_VERSION from below code and added time()

 wp_enqueue_style( 'themename-style', get_stylesheet_uri(), array(), time() );

and I got my output it is displaying like

<link rel='stylesheet' id='main-styles-css'  href='http://test.com/wp-content/themes/themename/style.css?ver=1597849143' media='' />
7
  • The only possible problem with this is that the file might never get cached now, because time constantly changes, which could have an impact on performance. At least with filemtime, which also gives you a timestamp, it only changed when the file changed. If it returned the WP version before, that means filemtime returned false, and that means that wp_enqueue_style defaults to the WP version. So the function didn't work properly or didn't point to the style.css file correctly. Maybe try it again and print out the stylesheet path to make sure it looks right.
    – NightHawk
    Aug 19, 2020 at 18:44
  • @NightHawk, Yes you are right now the file never gets cached and that I want it. I got your point. I will try filemtime. Aug 19, 2020 at 18:50
  • I have to use something like this? wp_enqueue_style('main-styles', get_template_directory_uri() . '/style.css', array(), filemtime(get_template_directory() . '/style.css'), false); Aug 19, 2020 at 18:52
  • That looks about right, and no, you don't have to use that. You could manually increment the version each time you want the cache to be refreshed, too. Or if you version your plugin/theme, you could use that version. The idea is just that it's a number that changes when you need/want it to change.
    – NightHawk
    Aug 19, 2020 at 19:32
  • @NightHawk, Why I am doing this, Because ever time it's creating the cache(I am not using any plugin for cache) , I refresh the URL, HIt the URL, even refresh the css from view source but still not reflecting. Sometimes I notice css updated from view source but changes are not showing on the website. Aug 19, 2020 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.