A child theme in WordPress is a theme that inherits the functionality, styling, and features of another theme, called the parent theme. It allows you to modify or extend the parent theme without directly editing its files.
Why use a child theme?
Mostly it is used to be able to customize any existing theme. Say you want to modify a header.php file just a bit – if you do it in a parent theme, and run updates later, your header.php file will be overwritten. So you use a child theme, to override some single files from that parent theme.
- Preserve updates – If you modify a parent theme directly, updates will overwrite your changes. A child theme ensures updates don’t erase customizations.
- Customization without risk – You can tweak styles, templates, and functions without affecting the core theme.
- Easy reversion – If something goes wrong, you can always revert to the parent theme.
- Modular development – Developers can make enhancements without altering the original theme, ensuring a structured workflow.
How a child theme works
A basic, out-of-the-box child theme typically consists of:
- A style.css file with a header linking it to the parent theme.
- A functions.php file to enqueue styles and add custom functionality.
That’s the minimum you need to have it detected as a child theme.
WordPress automatically checks for template files in the child theme first. If a file (e.g.,
header.php
) exists in the child theme, it overrides the parent theme’s file. If it’s missing, WordPress falls back to the parent theme’s file.Make sure the file structure in child theme matches the one in the parent theme.
For example, if the file you want to modify is located in ../themes/astra/inc/form.php, you’ll need to create a directory called “inc” to the child theme as well: ../themes/astra-child-theme/inc/form.php
Lets create a child theme
Create a new folder in /wp-content/themes/
, e.g., my-child-theme
.
Add a style.css
file inside with this content: css
/*
Theme Name: My Child Theme
Template: parent-theme-folder-name
*/
Add a functions.php
file to enqueue the parent theme’s styles:
<?php
function my_child_theme_styles() {
wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'my_child_theme_styles');
Go to wp-admin > Themes and activate your child theme
How to modify any file
To modify a file like header.php
from the parent theme in your child theme, follow these steps:
- Locate the
header.php
file- Go to your WordPress installation directory.
- Navigate to
/wp-content/themes/your-parent-theme/
. - Find the
header.php
file.
- Copy
header.php
to the Child Theme- Copy the
header.php
file from the parent theme and paste it into the child theme directory (your-child-theme/header.php
).
- Copy the
- Modify the
header.php
File in the Child Theme- Open the copied
header.php
file in a code editor. - Make your changes (e.g., adding a custom logo, modifying navigation, or inserting extra code).
- Example: Adding a custom message before the header:
<div class="custom-message">Welcome to My Custom Theme!</div>
- Open the copied
Now, if your parent theme receives updates, your custom message won’t be missing.