2

After reading several posts here (including [1] and [2]) and testing with Wordpress 4.5.3 using Twenty Sixteen as parent theme, I think the following (in functions.php) code must be correct:

function childtheme_enqueue_styles() {
  $parent_style = 'twentysixteen-style';
  wp_enqueue_style(
    $parent_style,
    get_template_directory_uri() . '/style.css'
  );
  wp_enqueue_style(
    'childtheme-style',
    get_stylesheet_directory_uri() . '/style.css',
    array( $parent_style ),
    wp_get_theme()->get('Version')
  );
}

add_action( 'wp_enqueue_scripts', 'childtheme_enqueue_styles' );

It loads the parent stylesheet (with parent theme version number) and then the child theme stylesheet (with child theme version number). This seems to enable independent cache busting.

I also read the documentation and was so confident that I updated it to reflect my findings ...

PS! My reputation is too low to comment or to post a new answer to one of the existing posts, so that is why I'm "forced" to post this as a new question. Anyway, I think the question is OK - and the docs get reviewed if I'm wrong.

  • I prefer more dynamic way of output version (for eg: using filetime() of the child theme css ) – bravokeyl Aug 8 '16 at 13:36
  • @hansfn filetime goes to the file system on each request. Not really the best thing to do from a performance perspective – kraftner Aug 8 '16 at 15:08
  • 1
    @kraftner Using filemtime (not filetime) is of course not an option unless while you are developing a theme. – hansfn Aug 8 '16 at 16:43
  • Isn't the above code working as expected? Is the ressource loaded from cache even with an incremented child theme version? – Hans Spieß Mar 10 '17 at 8:44
0

twentysixteen is doing it in a bad way, surprisingly. They should first register the parent themes styles and scripts with a correct version attribute with wp_register_style in child themes you could then just do.

wp_enqueue_style( $parent_style );

To get the correct version of the style for cache-busting the parent themes style. But you also may be confusign something as you only need that when the parent themes updates and that happens not very much so its really not that important for the parent theme but more for the theme you currently work on. And in case you do not want to update the version all the time in development I have something for that.

There should also be a way to read the parent themes version and pass it for ver. Well that is probably what you are asking ;) Do know know.

May be helpful: I recently came up with a function that uses filemtime only in development

<?php
namespace Nextgenthemes\NativeLazyloadPolyfill;

const VERSION     = '0.9.12';
const VERY_LATE   = PHP_INT_MAX - 5;
const PLUGIN_FILE = __FILE__;
const PLUGIN_DIR  = __DIR__;

init();

function init() {

    $ns = __NAMESPACE__;

    add_action( 'wp_enqueue_scripts', "$ns\\action_wp_enqueue_scripts" );
}

function action_wp_enqueue_scripts() {

    wp_register_script(
        'nextgenthemes-loading-attribute-polyfill',
        plugins_url( 'node_modules/loading-attribute-polyfill/loading-attribute-polyfill.min.js', PLUGIN_FILE ),
        array(),
        ver( VERSION, 'node_modules/loading-attribute-polyfill/loading-attribute-polyfill.min.js', PLUGIN_FILE ),
        false
    );

    wp_enqueue_script( 'nextgenthemes-loading-attribute-polyfill' );
}

function ver( $ver, $path, $file ) {

    if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
        $ver = filemtime( trailingslashit( dirname( $file ) ) . $path );
    }

    return $ver;
}

I actually to not use it in that plugin but it was easy to copy paste and cut down the code for SO. Its modeled after plugins_url with a version as first parameter. In a parent child theme scenario you would want to have a constant of the functions.php file of your theme and pass that to the function.

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.