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/folder | Required | Purpose |
|---|---|---|
generatepress-child/ | Yes | Child theme folder under wp-content/themes/ |
style.css | Yes | Theme header + parent link (Template: generatepress) |
functions.php | Yes | Enqueue styles and register hooks/filters |
screenshot.png | Optional | Admin 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
Step 5: Verify the Parent Link
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
| Practice | Why |
|---|---|
| Start minimal, then add structure | Easier to debug |
Use filemtime() for cache-busting in staging | Prevents stale CSS |
| Keep theme name and folder name stable | Avoids deploy mismatches |
| Commit early to Git | Rollback and audit trail |
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Theme not listed | Missing Theme Name: header | Fix style.css header and re-check |
| Activation fails | Wrong folder name | Ensure folder is generatepress-child |
| No styles | Parent not enqueued | Confirm enqueue code in functions.php |
| White screen | PHP syntax error | Check /usr/local/lsws/logs/error.log |
Hands-On
- Create the child theme via CLI.
- Activate it and confirm
stylesheetandtemplatevalues. - 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
- Next: style.css Header & Metadata
- Related: Child Theme Setup (overview)