← Tous les articles

WordPress child themes: how to create one and customise your site without losing changes

Do your changes vanish every time the theme updates? A child theme is the fix. Creating one by hand or with WP-CLI, loading styles correctly, overriding templates and functions, lost Customizer settings, block themes: the full method and the pitfalls we run into.

WordPress child themes: how to create one and customise your site without losing changes

Key points

  • A child theme inherits everything from its parent and holds only your changes, so parent updates no longer wipe them out.
  • Two files get you started (style.css and functions.php), but the load order of styles and functions causes most of the bugs.
  • Activating the child can make Customizer settings look lost, and on a block theme, Site Editor changes live in the database, not in files.

It’s a situation we still see all the time when we take over a site: someone edited the theme’s header.php or stylesheet directly to add a script, change a colour or remove a link. Everything works until the theme updates and those changes silently disappear. The opposite reflex, never updating the theme again, is worse, because it leaves security holes open. WordPress has had the right answer for years: a child theme. Here’s how to build one properly and, above all, how to avoid the traps that make people think it “doesn’t work”.

How it works: a theme that inherits from another

A child theme is an almost empty theme that declares a parent. WordPress uses the child’s files where they exist and the parent’s for everything else. You only copy what you change, and the parent can be updated freely.

There are three mechanisms to understand:

  • Templates (header.php, single.php, template-parts files…): if the child has a file with the same name in the same place, it replaces the parent’s.
  • functions.php: the child’s doesn’t replace the parent’s; it loads in addition, and before it.
  • The stylesheet: the child’s style.css is required (its header declares the theme), but whether it’s actually loaded on the front end depends on how the parent enqueues its own styles.

A child theme isn’t always the right tool. For a handful of CSS rules, the Customizer’s Additional CSS field is enough. For features unrelated to design (a custom post type, a business rule), a small custom plugin is better, because it survives a theme change. A child theme is the right choice as soon as you touch appearance or templates.

Creating the child theme in two minutes

With WP-CLI, one command creates the folder and starter files:

# Creates wp-content/themes/astra-child with style.css and functions.php
wp scaffold child-theme astra-child --parent_theme=astra 
   --theme_name="Astra Child" --author="Your agency"

# Check, then activate
wp theme list --fields=name,status,version
wp theme activate astra-child

By hand, create a folder in wp-content/themes/ (for example astra-child) containing a style.css that starts with this header:

/*
 Theme Name:   Astra Child
 Template:     astra
 Description:  Site customisations, safe from updates
 Version:      1.0.0
 Text Domain:  astra-child
*/

/* Your CSS overrides go here */

The line that matters is Template: it must hold the parent’s folder name, not its marketing name. It’s the most common mistake: “Template: Astra Pro” instead of “Template: astra”, and WordPress reports a missing parent theme. Add a screenshot.png (1200 × 900 pixels) so you can recognise it in the admin, then activate it under Appearance > Themes.

Put the folder under Git from day one: from now on, it holds all the site-specific work.

Loading styles in the right order

This is where most child themes “break” the site on the first try: either the parent’s design vanishes or the child’s rules don’t apply. It all depends on which function the parent uses to load its style.css:

  • if the parent uses get_stylesheet_uri(), that now points to the child: the child’s stylesheet is loaded and the parent’s disappears;
  • if the parent uses get_template_directory_uri(), its own stylesheet loads, but the child’s isn’t loaded at all;
  • many modern themes load styles from compiled files (assets/css/…) and already handle child themes correctly.

Before writing anything, view a page’s source and check which stylesheets are loaded. Then fill the gap in the child’s functions.php:

<?php
// Child theme functions.php
add_action( 'wp_enqueue_scripts', function () {
    $parent = wp_get_theme( get_template() );

    // 1. The parent stylesheet, unless the parent already loads it itself
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css',
        array(), $parent->get( 'Version' ) );

    // 2. The child stylesheet, AFTER the parent, versioned to bust caches
    wp_enqueue_style( 'child-style', get_stylesheet_uri(),
        array( 'parent-style' ), wp_get_theme()->get( 'Version' ) );
}, 20 );

If the parent already loads its stylesheet, drop the first call and make the child depend on the handle the parent uses (visible in the <link> tag’s id attribute, minus the -css suffix). Avoid the old @import approach in style.css: it forces sequential downloads and delays rendering.

The version number passed as the last argument prevents a silly trap: after a CSS change, browsers and page caches keep serving the old file. Bump the version in the style.css header with every release.

Overriding templates and functions without copying everything

To change a template, copy the parent’s file into the child at the same path (for example template-parts/content-single.php) and edit the copy. Copy as few files as possible: every copied template stops receiving the parent’s fixes. On our projects we note, at the top of each copied file, which parent version it came from, so we can diff it after a major update.

When the parent offers hooks (good themes offer plenty), hooking in is almost always better than copying a template. And to remove something the parent adds, mind the load order:

<?php
// The child's functions.php loads BEFORE the parent's:
// a remove_action() written straight here would find nothing to remove.
add_action( 'after_setup_theme', function () {
    // Same hook, same function, same priority as in the parent
    remove_action( 'wp_footer', 'parent_theme_credits', 10 );
} );

// Utiliser les bons chemins
require get_stylesheet_directory() . '/inc/shortcodes.php';      // child file
$logo = get_template_directory_uri() . '/assets/img/logo.svg';   // parent file

The remove_action() call must match the parent’s function and priority exactly; if it does nothing, one of the two almost always differs, or the parent used an anonymous function, which can’t be removed this way. Some themes wrap their functions in if ( ! function_exists( ... ) ): since the child loads first, declaring a function with the same name is enough to replace it.

One last habit: get_stylesheet_directory() is the child’s folder, get_template_directory() the parent’s. Mixing them up produces confusing “file not found” errors.

Two traps after activation: settings and block themes

The Customizer looks reset. Logo, colours, menu locations: many themes store these in a per-theme option (theme_mods_ followed by the folder name). When you activate the child, WordPress reads an empty option. Some themes store their settings elsewhere and aren’t affected; otherwise, copy them across with WP-CLI, on a staging copy first:

# Customizer settings are stored per theme
wp option get theme_mods_astra --format=json > mods-parent.json

# Copy them to the child theme (after checking the file)
wp option update theme_mods_astra-child --format=json < mods-parent.json

Block themes work differently. With a theme like Twenty Twenty-Four, a child mostly contains a theme.json that’s merged with the parent’s: you only declare what changes.

{
  "$schema": "https://schemas.wp.org/trunk/theme.json",
  "version": 3,
  "settings": {
    "color": {
      "palette": [
        { "slug": "primary", "color": "#1d4ed8", "name": "Primary" }
      ]
    }
  },
  "styles": {
    "typography": { "lineHeight": "1.6" }
  }
}

Templates are overridden by placing a file with the same name in templates/ or parts/. But beware: changes made in the Site Editor are saved in the database and take precedence over files, the parent’s and the child’s alike. If a template you edited on disk changes nothing on screen, check Appearance > Editor for a customised version and reset it. To move Site Editor tweaks back into files, the official Create Block Theme plugin can export them.

FAQ

Do I need a child theme if I only change CSS?

For a few rules, the Customizer’s Additional CSS field is enough and survives updates. Once the CSS grows, or you touch templates or functions.php, a child theme is better because it can be versioned and deployed like code.

Does a child theme slow the site down?

Not measurably: WordPress reads a few extra files. What slows things down is loading the parent CSS through @import or needlessly copying heavy templates.

Can I make a child theme of a child theme?

No, WordPress supports a single level of inheritance. If your site already uses a child theme supplied by the theme vendor, work inside it or move your changes into a plugin.

Changes that vanish with every update, or a theme modified beyond maintainability? We clean things up: a proper child theme, versioned code and updates tested before they reach production.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →