Learn how to create and use a child theme in WordPress to customize an existing theme safely without losing your changes when the parent theme is updated.
A child theme is a WordPress theme that inherits functionality, styles, and other features from another theme called the parent theme.
You can use a child theme to customize your website while keeping the original parent theme files unchanged.
A parent theme is the original WordPress theme that provides the main design and functionality for a website.
A child theme is usually stored in the
wp-content/themes/ directory.
wp-content/
└── themes/
├── astra/
│ ├── style.css
│ ├── functions.php
│ └── ...
│
└── astra-child/
├── style.css
├── functions.php
└── page-services.php
In this example, astra is the parent theme and
astra-child is the child theme. The folder names
are examples; use the actual parent theme directory name for
your own website.
Open your WordPress installation directory and navigate to:
wp-content/themes/
Create a new folder inside this directory. For example, if your
parent theme folder is named astra, create:
astra-child
You can create the folder using:
Inside the child theme folder, create a file named
style.css.
/*
Theme Name: Astra Child
Theme URI: https://example.com/
Description: A child theme for Astra
Author: Your Name
Template: astra
Version: 1.0.0
Text Domain: astra-child
*/
/* Add your custom CSS below */
Understand the important fields:
| Field | Purpose |
|---|---|
Theme Name |
The name displayed in the WordPress admin area. |
Description |
A short description of the child theme. |
Author |
The name of the theme creator. |
Template |
The exact parent theme directory name. |
Version |
The version number of the child theme. |
Text Domain |
The text domain used for translation strings. |
Template value must match the parent theme's
actual folder name exactly. If your parent folder is
twentytwentyfive, use
Template: twentytwentyfive.
The functions.php file lets you add PHP functionality
to your child theme. A common task is loading the child theme's
stylesheet.
<?php
function astra_child_enqueue_styles() {
wp_enqueue_style(
'astra-child-style',
get_stylesheet_uri(),
array(),
wp_get_theme()->get('Version')
);
}
add_action(
'wp_enqueue_scripts',
'astra_child_enqueue_styles'
);
This example loads the child theme's
style.css file. It does not explicitly load the
parent stylesheet. Depending on the parent theme, its existing
stylesheet-loading behavior, and the dependencies required,
you may need to configure the enqueue logic differently.
You can now add your own CSS rules to the child theme's
style.css file.
/* Customize the main heading */
h1 {
color: #6a11cb;
font-size: 36px;
font-weight: bold;
}
/* Customize paragraphs */
p {
line-height: 1.8;
}
/* Customize buttons */
button,
.wp-block-button__link {
border-radius: 8px;
}
Save the file and view your website after activating the child theme. The changes will apply where the selectors match.
Follow these steps to activate your child theme:
A child theme can override many classic PHP template files by providing a file with the same relative path and filename. WordPress will use the child theme's version when that template is selected.
Suppose the parent theme contains:
astra/
└── page.php
Your child theme contains:
astra-child/
└── page.php
When WordPress selects page.php, it can use
the child theme's version instead of the parent's version.
Keep the original parent file unchanged.
You can also create a page-specific template in your child theme.
For example, create page-contact-us.php to provide
a layout for a page whose slug is contact-us.
<?php
get_header();
?>
<main class="container py-5">
<h1>Contact Us</h1>
<p>Get in touch with our team.</p>
<p>
Email: info@example.com
</p>
</main>
<?php get_footer(); ?>
Place this file in your child theme's root directory. If the
page slug is contact-us, WordPress can select this
template according to the classic page template hierarchy.
Child themes can be used with classic themes and block themes, but the files you customize may be different.
| Classic Theme | Block Theme |
|---|---|
Commonly uses PHP templates such as page.php. |
Commonly uses HTML templates such as templates/page.html. |
| Uses PHP template tags and WordPress hooks. | Uses block markup and template parts. |
| Can customize PHP templates and styles. | Can customize HTML templates, template parts, styles, and theme settings. |
Usually includes a style.css file and may include functions.php. |
Can use style.css, theme.json, and a templates/ directory. |
Template field.style.css file.Template field.functions.php file to enqueue your stylesheet appropriately.wp-content/themes/.style.css header identifies the child theme and its parent.functions.php file can add custom functionality.Choose the correct answer for each question.