How to Create a Child Theme in WordPress

Marek P. Dec 20, 2024 Theming
What’s the best way to change my website's look without losing my changes when I update my theme?
What are the steps to create a child theme in WordPress to safely modify a parent theme without losing customizations during updates?
Andy answered Dec 20, 2024

Why Use a Child Theme?

A child theme lets you customize your WordPress site while keeping the ability to update the parent theme safely. When you update the parent theme, your customizations remain intact.

Creating a Child Theme - Step by Step

1. Set Up the Child Theme Folder

First, create a new folder in your WordPress themes directory:

  • Go to wp-content/themes/
  • Create a new folder named yourtheme-child (replace "yourtheme" with your parent theme's name)

2. Create the Style File

Create a style.css file in your child theme folder with the required header information:

The essential style.css header for your child theme:

/*
Theme Name: Your Theme Child
Theme URI: https://yoursite.com/
Description: Child theme for Your Theme
Author: Your Name
Author URI: https://yoursite.com/
Template: yourtheme
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: yourtheme-child
*/

3. Create functions.php

Set up the parent theme styles enqueue:

Basic functions.php to load parent and child theme styles:

<?php
function my_child_theme_styles() {
    wp_enqueue_style( 'parent-style', 
        get_template_directory_uri() . '/style.css' 
    );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array('parent-style')
    );
}
add_action( 'wp_enqueue_scripts', 'my_child_theme_styles' );

Best Practices

  1. Naming Convention: Use parentthemename-child format for folder name
  2. File Organization: Keep child theme files organized in the same structure as parent
  3. Minimal Override: Only copy files you need to modify
  4. Use Hooks: Prefer using action and filter hooks over direct file overrides

Security Considerations

  1. Always download parent themes from trusted sources
  2. Keep both parent and child themes updated
  3. Use proper file permissions (755 for folders, 644 for files)
  4. Never modify core WordPress files

Common Pitfalls

  1. Wrong template name in style.css header
  2. Incorrect file paths in functions.php
  3. Not checking if parent theme exists
  4. Overriding too many parent theme files

Helpful Plugins

  1. Child Theme Configurator

    • Makes child theme creation point-and-click simple
    • Plugin Page
  2. Child Theme Generator

    • Creates child themes directly from WordPress admin
    • Plugin Page

Advanced Configuration

Add parent theme script dependencies:

<?php
function my_child_theme_scripts() {
    wp_enqueue_script(
        'child-main',
        get_stylesheet_directory_uri() . '/js/main.js',
        array('jquery'),
        '1.0.0',
        true
    );
}
add_action( 'wp_enqueue_scripts', 'my_child_theme_scripts' );

Testing Your Child Theme

  1. Activate the child theme in WordPress admin
  2. Check frontend styling
  3. Test responsiveness
  4. Verify all functionality works
  5. Update parent theme to ensure changes persist

Remember to always backup your site before making theme changes, and test your child theme thoroughly in a staging environment first.