Themes WordPress

Working with Child Themes in WordPress

Recently, I had an issue that I could not figure out for the life of me. I had been working on a new theme and instead of creating from scratch, I decided to use a child theme to speed up delivery. This was working, except for the fact that I noticed that the style was being called twice.

In the Twenty Nineteen theme, they have a tidbit of code in “functions.php:220” that calls the get_stylesheet_uri method:

wp_enqueue_style( 'twentynineteen-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );

What this ends up doing is enqueueing my theme again, after the parent theme is called. This happens because get_stylesheet_uri method only loads the current theme, instead of including the parent from where it was called. No matter what method I used I couldn’t get the key value for “twentynineteen-styles” to be dequeued.

So, instead of trying to delete it, I decided to use it. If you notice in the code below, I am calling twentynineteen-style again, but this time I am making sure that the parent theme is being called first, along with any other files that we may want to call.

 * Enqueue scripts and styles.
public static function scripts(): void
    // Add custom fonts, used in the main stylesheet.
    wp_enqueue_style('childtheme-fonts', static::fontsUrl());

    // Enqueue the parent theme stylesheet
        get_template_directory_uri() . '/style.css'

    // Enqueue my theme style over the parent theme's key
        ['parent-style', 'childtheme-fonts'],
        wp_get_theme()->get( 'Version' )

 * Called Elsewhere
add_action('wp_enqueue_scripts', [__CLASS__, 'scripts']);

The key point is that instead of trying to add another key, like different versions of the documentation say, I am simply just replacing the parent theme with my own. And, I hope that if you find this that it reduces the amount of time you will have to spend trying to figure it out.