Skip to main content

Create Child Theme via CLI

warning

Run this on staging first. A typo in style.css or functions.php can break theme activation or site rendering.

Prerequisites

File/folderRequiredPurpose
generatepress-child/YesChild theme folder under wp-content/themes/
style.cssYesTheme header + parent link (Template: generatepress)
functions.phpYesEnqueue styles and register hooks/filters
screenshot.pngOptionalAdmin thumbnail

Safety Notes

  • CLI creation is repeatable and works over SSH.
  • You can store the child theme in Git and deploy it like application code.
  • A minimal child theme is easier to debug than a pre-bloated starter.

How It Works

WordPress scans wp-content/themes/ for folders with a valid style.css header. The Template: header tells WordPress which parent theme to inherit from.

flowchart LR
STYLE[style.css header] --> WP[WordPress theme registry]
WP --> PARENT[Parent: generatepress]
WP --> CHILD[Child: generatepress-child]

Step-by-Step Creation

Step 1: Create the Theme Folder

create-child-theme-folder.sh
cd /var/www/html
mkdir -p wp-content/themes/generatepress-child
ls -lah wp-content/themes | grep generatepress

Step 2: Write style.css (Minimum Header)

write-style-css.sh
cat > wp-content/themes/generatepress-child/style.css <<'EOF'
/*
Theme Name: GeneratePress Child
Template: generatepress
Version: 0.1.0
Text Domain: generatepress-child
*/
EOF

Step 3: Write functions.php (Enqueue Parent + Child)

write-functions-php.sh
cat > wp-content/themes/generatepress-child/functions.php <<'EOF'
<?php

add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_style(
'generatepress-parent',
get_template_directory_uri() . '/style.css'
);

wp_enqueue_style(
'generatepress-child',
get_stylesheet_directory_uri() . '/style.css',
array( 'generatepress-parent' ),
filemtime( get_stylesheet_directory() . '/style.css' )
);
} );
EOF

Step 4: Activate the Child Theme

activate-child-theme.sh
cd /var/www/html
wp theme activate generatepress-child
wp option get stylesheet
wp option get template

Expected output:

generatepress-child
generatepress
verify-template-header.sh
cd /var/www/html
grep -n "^Template:" wp-content/themes/generatepress-child/style.css

Expected output:

Template: generatepress

Optional Configuration

Example 1: Roll Back to Parent Theme (Emergency Command)

rollback-to-parent.sh
cd /var/www/html
wp theme activate generatepress
wp option get stylesheet

Example 2: Package the Child Theme as an Artifact

package-child-theme.sh
cd /var/www/html
tar -czf /tmp/generatepress-child.tgz -C wp-content/themes generatepress-child
ls -lh /tmp/generatepress-child.tgz

Best Practices

PracticeWhy
Start minimal, then add structureEasier to debug
Use filemtime() for cache-busting in stagingPrevents stale CSS
Keep theme name and folder name stableAvoids deploy mismatches
Commit early to GitRollback and audit trail

Troubleshooting

SymptomCauseFix
Theme not listedMissing Theme Name: headerFix style.css header and re-check
Activation failsWrong folder nameEnsure folder is generatepress-child
No stylesParent not enqueuedConfirm enqueue code in functions.php
White screenPHP syntax errorCheck /usr/local/lsws/logs/error.log

Hands-On

  1. Create the child theme via CLI.
  2. Activate it and confirm stylesheet and template values.
  3. Package it to /tmp/generatepress-child.tgz.

Quick Reference

child-theme-cli-cheatsheet.sh
cd /var/www/html
mkdir -p wp-content/themes/generatepress-child
wp theme activate generatepress-child
wp option get stylesheet

What's Next