3 comments on “WordPress Child Themes: 4 ways to avoid inefficient CSS import

  1. I just found your post hoping to find a better solution for @import, but didn’t find a flexible one. I was thinking about including the style.css with a function and hook in the child functions.php See this one:

    function add_parent_styles() {
    wp_register_style(‘parent_style’, get_template_directory_uri() . ‘/style.css’, array(), 0, ‘all’);
    wp_enqueue_style(‘parent_style’);
    }

    add_action(‘wp_enqueue_scripts’, ‘add_parent_styles’);

    I am still working on the order of this style compared to other style files and I probably need to change some url() paths in the style.css to something like url(‘../parent_theme/img/image.png’).

    Maybe others can add something to this.

  2. @Thomas thanks for the input; I’m sure wp_enqueue_style would be the considered the “correct” way to add styles, and at some point I may give your code a try.

    To date I’ve not had to worry about CSS or Javascript ordering etc; so I have no experience of enqueing and I am unable to make any suggestions.

    I assume you’ve read it, but others may find WordPress Codex wp_enqueue_style (and the articles it lists under Resources) useful.

  3. Thomas is correct – you can add this to a child functions.php and it works.

    The WP function ‘get_template_directory_uri’ targets the parent folder, while ‘get_stylesheet_directory_uri’ targets self. So you could use this to swap enqueued stylesheets in the child.

    I had to change my parent theme’s Javascript enqueueing to ‘get_template_directory_uri’ so it would work in the child.

Your Comments & Questions

email address is optional and is not displayed