Child theme starter
A minimal child theme with a correct stylesheet header and a functions file that loads parent and child styles with cache busting.
WordPressThemingchild-themeenqueuecustomisationstarter
Code
/*
Theme Name: Acme Theme Child
Theme URI: https://example.com/
Description: Child theme for Acme Theme. Put overrides here, not in the parent.
Author: Acme Studio
Template: acme-theme
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: acme-theme-child
*/
/* Your overrides go below. */
<?php
/**
* Child theme bootstrap.
*
* @package Acme_Theme_Child
*/
defined( 'ABSPATH' ) || exit;
/**
* Load the parent stylesheet first, then the child's, each versioned by file time.
*/
function acme_child_enqueue_styles(): void {
$parent = wp_get_theme( get_template() );
wp_enqueue_style(
'acme-parent',
get_template_directory_uri() . '/style.css',
array(),
$parent->get( 'Version' )
);
wp_enqueue_style(
'acme-child',
get_stylesheet_uri(),
array( 'acme-parent' ),
(string) filemtime( get_stylesheet_directory() . '/style.css' )
);
}
add_action( 'wp_enqueue_scripts', 'acme_child_enqueue_styles' );
/**
* Load child translations. The parent's strings keep using the parent's domain.
*/
function acme_child_setup(): void {
load_child_theme_textdomain( 'acme-theme-child', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'acme_child_setup' );
/**
* Example override: change the excerpt length for this site only.
* The parent should use apply_filters(), and the child hooks in at a later priority.
*/
add_filter( 'excerpt_length', static fn(): int => 32, 20 );
How it works
What it solves
Editing a theme directly loses your changes at the next update. A child theme inherits everything from its parent, and only the files you override live in it, so you can customise safely and still receive upstream fixes.
How it works
The Template header names the parent theme's folder, which is what makes WordPress treat this as a child. The stylesheet itself can be almost empty because it is loaded through code. The functions file enqueues the parent stylesheet first using the parent's version number, then the child's with a dependency on the parent and a file modification time as the version, so edits show immediately without a stale cache.
The child's functions.php runs before the parent's.
Adapting it
Copy a template such as header.php from the parent into the child folder to override it. Use filters and actions for logic changes. Replace acme-theme in the header with the real parent folder name. If the parent already enqueues its own stylesheet, remove the parent enqueue here to prevent loading it twice.
Pitfalls
- The
Templatevalue is the parent's directory name, not its display name. - Do not use
@importin the stylesheet; it blocks rendering. - Parent functions wrapped in
function_existscan be redefined, others cannot. - Block themes need parent
theme.jsonoverrides in the child's owntheme.jsoninstead.
How to use it
Create a folder next to the parent theme, for example wp-content/themes/acme-child.
Add the two files, then activate Acme Theme Child in Appearance, Themes.
Add CSS overrides to the child stylesheet.
Licence: GPL-2.0-or-later. This snippet comes from WordPress code and is GPL-2.0-or-later.