A WordPress child theme allows you to customize an existing theme without modifying the original theme files. This is important because changes made directly in the parent theme may be lost during theme updates.
WP-CLI makes it possible to create a child theme quickly from the command line.
Table of Contents
What Is a Child Theme?
A child theme is a theme that inherits the design and functionality of another theme, called the parent theme.
You can use a child theme to safely modify:
- CSS styles
- Template files
- Theme functions
- Layout elements
The parent theme can still be updated without overwriting your custom changes.
Before You Begin
You need:
- SSH access to the server
- WP-CLI installed
- WordPress already installed
- The parent theme already installed
Connect to your server via SSH and go to your WordPress installation directory:
cd public_html
The exact directory may differ depending on where WordPress is installed.
Check the Parent Theme Name
Before creating the child theme, check the installed themes:
wp theme list
Find the parent theme slug in the name column.
For example:
twentytwentyfour
astra
generatepress
You will need this slug in the next step.
Create the Child Theme
Use the following command:
wp scaffold child-theme my-child-theme --parent_theme=parent-theme-slug
Replace:
my-child-themewith the name of your child theme folderparent-theme-slugwith the actual parent theme slug
Example:
wp scaffold child-theme astra-child --parent_theme=astra
WP-CLI will create the child theme directory and the required files automatically.
Activate the Child Theme
After creating the child theme, activate it with:
wp theme activate astra-child
You can confirm that it is active by running:
wp theme list
Edit the Child Theme Files
The child theme will be created in:
wp-content/themes/astra-child/
You can edit files such as:
style.cssfunctions.php
Use style.css for custom CSS and functions.php for custom PHP functions.
Test the Website
After activating the child theme:
- Open the website in your browser.
- Check that the layout loads correctly.
- Verify that custom styles are applied.
- Test key pages and features.
If something looks broken, switch back to the parent theme and review the child theme files.
Summary
Creating a WordPress child theme with WP-CLI is a fast and safe way to customize a website without editing the parent theme directly. Use wp scaffold child-theme, activate the new theme, and add your custom changes inside the child theme directory.