WordPress Tutorial
Lesson 40 of 50 (80%)
80% Complete

WordPress Child Themes

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.

Learning Objective: Understand parent and child themes, create a child theme, configure its stylesheet, activate it, and customize your website safely.

1. What Is a WordPress Child Theme?

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.

Example
Suppose your website uses the Astra theme. You want to change colors, add custom CSS, and create a custom page layout. Instead of directly modifying Astra's files, you can create and activate an Astra child theme.

2. What Is a Parent Theme?

A parent theme is the original WordPress theme that provides the main design and functionality for a website.

Parent Theme
  • Contains the original theme files.
  • Provides layouts and template files.
  • Can receive updates from its developer.
  • Should generally remain unmodified when using a child theme.
Child Theme
  • Inherits from a parent theme.
  • Can contain custom CSS and PHP templates.
  • Can override supported parent templates.
  • Helps keep customizations separate from parent updates.

3. Why Should You Use a Child Theme?

Main Benefits
  1. Safer customization: Keep custom code separate from the parent theme.
  2. Update protection: Custom files stored in the child theme are not normally overwritten by parent theme updates.
  3. Easy maintenance: Manage your customizations in one place.
  4. Template overrides: Replace supported parent theme templates with your own.
  5. Learning opportunity: Experiment with CSS, PHP, and WordPress template files.
Important: A child theme does not automatically protect every customization. Changes made directly to the parent theme can still be overwritten during updates. Some parent theme updates may also require changes to your child theme.

4. Child Theme Folder Structure

A child theme is usually stored in the wp-content/themes/ directory.

Example Folder Structure
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.

5. Step 1: Create the Child Theme Folder

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:

Folder Name
astra-child

You can create the folder using:

  • Your hosting control panel's File Manager.
  • An FTP client.
  • Your local development environment's file manager.

6. Step 2: Create the style.css File

Inside the child theme folder, create a file named style.css.

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.
Remember: The Template value must match the parent theme's actual folder name exactly. If your parent folder is twentytwentyfive, use Template: twentytwentyfive.

7. Step 3: Create the functions.php File

The functions.php file lets you add PHP functionality to your child theme. A common task is loading the child theme's stylesheet.

functions.php
<?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.

Important: Do not copy a generic stylesheet enqueue snippet blindly into every child theme. Check the parent theme's documentation and make sure styles load in the correct order.

8. Step 4: Add Custom CSS

You can now add your own CSS rules to the child theme's style.css file.

Example: Customize Headings
/* 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.

9. Step 5: Activate the Child Theme

Follow these steps to activate your child theme:

Activation Steps
  1. Log in to the WordPress admin dashboard.
  2. Open Appearance → Themes.
  3. Find your child theme, such as Astra Child.
  4. Click Activate.
  5. Open your website in a browser.
  6. Check the design and functionality.
Before switching themes on a live website, take a backup and test the child theme on a staging site. Theme switching can affect menus, widgets, layouts, and other theme-dependent settings.

10. Override a Parent Theme Template

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.

Example

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.

11. Add a Custom Page Template

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.

page-contact-us.php
<?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.

12. Child Themes and Block Themes

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.

13. Common Child Theme Mistakes

Mistakes to Avoid
  • Using the wrong parent folder name in the Template field.
  • Creating the child theme folder in the wrong location.
  • Forgetting to activate the child theme.
  • Loading styles in the wrong order.
  • Copying parent template files without checking compatibility after updates.
  • Editing parent theme files instead of the child theme.
  • Failing to test changes before deploying them to a live website.

14. Practical Assignment

Your Task
  1. Install or select a parent theme on a test WordPress website.
  2. Create a folder for its child theme.
  3. Create the child theme's style.css file.
  4. Add the correct parent folder name in the Template field.
  5. Create a functions.php file to enqueue your stylesheet appropriately.
  6. Activate the child theme from the dashboard.
  7. Change a heading's color using custom CSS.
  8. Create a custom page template and test it.

15. Lesson Summary

  • A child theme inherits features from a parent theme.
  • Child themes help separate customizations from parent theme files.
  • The child theme folder is usually inside wp-content/themes/.
  • The style.css header identifies the child theme and its parent.
  • The functions.php file can add custom functionality.
  • Child themes can override supported templates.
  • Backups and staging tests are important before changing a live theme.

Lesson 40 Quiz: WordPress Child Themes

Choose the correct answer for each question.

1. What is a child theme in WordPress?

2. Which field in style.css specifies the parent theme folder?

3. Where are WordPress themes usually stored?