A child theme is a theme that inherits the functionality and styling of another theme, called the parent theme. Using a child theme allows you to modify the parent theme without losing your changes when the parent theme is updated.
How to create a WordPress child theme from scratch?
Here are the four steps you need to take to create your child theme:
- Create a child theme folder: Create a folder within the themes folder of your WordPress installation, located at
wp-content/themes
. For example:wp-content/themes/mychildtheme
- Add a stylesheet: Within the added folder, add a CSS file called
style.css
. - Add the code below to
style.css
to tell WordPress that it’s a child theme:
/*
Theme Name: Name Of Your Child Theme
Template: parent-theme-folder
*/
This snippet can be modified, but the following information is required:
- Theme Name – This should be the (unique) name of your child theme
- Template – This is the name of the parent theme directory.
A more detailed version of a style.css
file would look like this:
/*
Theme Name: Name Of Your Child Theme
Description: Description of your child theme.
Author: Jane Doe
Author URI: http://example.com
Template: parent-theme-folder
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready
Text Domain: your-text-domain
*/
- Add a file called
functions.php
: Within the child theme folder you added in step 1, you need to add another file calledfunctions.php
- Within
functions.php
, add the following code to enqueue the parent stylesheets. Be sure to use the same handle name as the parent theme does for the parent styles.
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
wp_enqueue_style( 'child-stylesheet-handle',
get_stylesheet_uri(),
array( 'parent-stylesheet-handle' ), // Should be the same handle as the parent theme
wp_get_theme()->get( 'Version' ) // This only works if you have Version defined in the style header.
);
}
- Install and activate your child theme: Save all the files, and install your child theme. To install a WordPress child theme, follow the standard theme installation process. You can either use FTP to copy the child theme folder to a remote site, or create a zip file of the folder and upload it via WP Admin. To do the latter, navigate to Appearance > Themes > Add New and upload the zip file here. After uploading the child theme, the theme should show up under Appearance > Themes. From here you can activate the theme by clicking on the theme and then clicking Activate.
That’s it! Your child theme is now ready to use. You can modify the child theme’s files to customize the appearance and functionality of your WordPress site.
Summary
The file structure of your child theme will look like this (at the minimum):
wp-content/themes
/child-theme-folder
/style.css
/functions.php
How to override template files using a WordPress child theme?
Any file you add to your child theme will overwrite the same file in the parent theme! This means that if you add a file called header.php
, it will override the file header.php
in the parent theme. The same goes for all other files within the theme folder, like single.php
or archive.php
for example. This means you can create a copy of the theme files you want to change from the parent theme, and then make your modifications to the copied files. This way the files in the parent theme remain unchanged.