1

I am trying, essentially, to add the ?ver=1.0 tag to the end of my stylesheet, which I have included in my child theme's function file (enqueued). I have read the documentation in the codex but it does not seem to be applying in the source code, and it is not updating/cache breaking as intended when I apply updates.

The stylesheet itself is being included, however, the version is not applying, so I'm somewhat at a loose end here.

This is my current code:

add_action( 'wp_enqueue_scripts', 'kl_child_scripts',11 );
function kl_child_scripts() { 
    wp_deregister_style( 'kallyas-styles' );
    wp_enqueue_style( 'kallyas-styles', get_template_directory_uri().'/style.css', '' , ZN_FW_VERSION );
    wp_enqueue_style( 'kallyas-child', get_stylesheet_uri(), array('kallyas-styles') , filemtime(get_stylesheet_directory() . '/style.css'));

N.B. It is the last line to which I am trying to apply the version. (kallyas-child). The previous line (kallyas-styles) does appear to have some form of version ZN_FW_VERSION, but that does not produce the desired effect.

filemtime(get_stylesheet_directory() . '/style.css') is intended to break cache and update the version number each time the file is saved. Edit: I do, now, believe that this code does in fact work, but the theme (as it is a pre-build) is preventing it from applying properly. I will update if and when I figure this out, hoping to get to talk to the theme developers.

0

3 Answers 3

1

The issue is that your are using filemtime which return an int and you should have a string. As you can read in the documentation :

$ver

(string|bool|null) (Optional) String specifying stylesheet version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added. Default value: false

so you may try this :

add_action( 'wp_enqueue_scripts', 'kl_child_scripts',11 );
function kl_child_scripts() { 
    wp_deregister_style( 'kallyas-styles' );
    wp_enqueue_style( 'kallyas-styles', get_template_directory_uri().'/style.css', '' , ZN_FW_VERSION );
    wp_enqueue_style( 'kallyas-child', get_stylesheet_uri(), array('kallyas-styles') , strval(filemtime(get_stylesheet_directory() . '/style.css')));

I added the strval that will convert the int to string

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

9 Comments

This does seem to return correctly, i.e. if I echo strval(filemtime(get_stylesheet_directory() . '/style.css')); it returns 1510140577 (and updates with new saves as intended). It does not, however, apply when calling the style.css file in the source code... not sure what's going on there!
@math let's first try putting something static .. put for exmaple the value "14" in the version and see if it wokrs
Ah... good call. This is also doing nothing. wp_enqueue_style( 'kallyas-child', get_stylesheet_uri(), array('kallyas-styles') , '4.0.0'); ...must be a problem with the theme setup, or the child theme?
@Matt and do you see version on other CSS flies ? as maybe this function is desactivated
it appears to be, yes. I am able to dequeue the file from that same function... : /
|
-1

Use below code

        define('ZN_FW_VERSION','1.0');
        add_action( 'wp_enqueue_scripts', 'kl_child_scripts',11 );
        function kl_child_scripts() { 
            wp_deregister_style( 'kallyas-styles' );
            wp_dequeue_style( 'kallyas-styles' );
            wp_enqueue_style( 'kallyas-styles', get_template_directory_uri().'/style.css', array() , ZN_FW_VERSION );
            wp_enqueue_style( 'kallyas-child', get_stylesheet_uri(), array('kallyas-styles') , ZN_FW_VERSION);
        }

Comments

-1

in functions.php I added

 wp_enqueue_style( 'kallyas-child',
        get_stylesheet_directory_uri() . '/style.css',
        array( 'kallyas-styles' ),
        filemtime(__DIR__.'/style.css')
    );

tested it at tmp.demos.rent-a-ninja.org

worked with divi theme

6 Comments

I had seen this way mentioned before but I would then have to manually update the version number each time I edited the CSS file, right? I am aiming for it to be done programmatically if possible.
since your approach does not work, I'm asking if your filemtime returns the propper value? you might also try an md5 of the file?!
It seems to return correctly, i.e. if I echo filemtime(get_stylesheet_directory() . '/style.css') it can return 1510140577. It does not, however, apply to the style.css tag in the source code.
why do you deregister the original parent theme?
you need to add the name of the person using @Matt if you want him to get notified about your message unless he may not see it
|

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.