Create a WordPress child theme in two files
When you customise a theme by editing its files, every theme update overwrites your changes. A child theme solves this: it takes all templates and styles from the parent theme, and you keep only the files you change.
Problem: Changes made directly in a theme are lost after the theme is updated.
Solution: Create a child theme: a folder with style.css (with the Template header) and functions.php (which loads the parent stylesheet). Copy into it only the templates you need to change.
style.css
Create a folder in wp-content/themes/, for example mytheme-child, and a style.css in it. The Template header is the folder name of the parent theme, not its display name:
/* Theme Name: My Theme Child Template: mytheme Version: 1.0.0 */ /* your CSS goes below */
functions.php
The child theme’s style.css is not enough: the parent stylesheet must be loaded too. Enqueue the parent first and make the child stylesheet depend on it, so the child rules come later and win:
<?php
add_action( 'wp_enqueue_scripts', 'mytheme_child_enqueue_styles' );
function mytheme_child_enqueue_styles() {
wp_enqueue_style(
'mytheme-parent-style',
get_template_directory_uri() . '/style.css',
array(),
wp_get_theme( 'mytheme' )->get( 'Version' )
);
wp_enqueue_style(
'mytheme-child-style',
get_stylesheet_uri(),
array( 'mytheme-parent-style' ),
wp_get_theme()->get( 'Version' )
);
}
Look at the parent’s functions.php first. Some themes already enqueue their stylesheet with get_stylesheet_uri(), which in a child theme points to the child’s file; then the parent CSS is missing and you load it as above. Other themes load the parent file themselves, and enqueueing it again under another handle would load it twice.
Unlike templates, functions.php of the child does not replace the parent’s one: both are loaded, the child first.
Activate and override templates
Go to Appearance → Themes and activate the child theme. Customizer settings and menu locations are stored per theme, so you may need to set some of them again.
To change a template, copy it from the parent into the child theme with the same path, for example single.php or template-parts/content.php, and edit the copy. WordPress looks for every template in the child theme first and falls back to the parent.
NOTE: Copy only the files you really change. Every copied template stops receiving fixes from parent theme updates, so the fewer copies, the less you have to compare after an update.