🥽 Beta testers needed! Stay up to date with the latest news from CTRLS.DEV

How to Use WP-CLI and Create WordPress Child Themes

Quickly and easily generate and initialize child themes using WP-CLI.



In this quick tutorial, I will share a PHP script I have put together that will allow you to extend parent themes and generate a child theme start files with a single WP-CLI command. You can use any existing theme that you have previously downloaded in the wp-content/themes/ folder.

🎉 hello@ctrls.dev:~/$ wp eval-file boilerplate.php -parent twentytwenty -name "2020 Child Theme" -description "This is my first child theme." -bootstrap true
Success! Child theme `twentytwenty-child` created successfully.

The Setup

There are two things that you need to have to execute the PHP script, generate and initialize a child theme structure.

  1. SSH access to your web server. (most hosts give you this information when you register your account)
  2. WP-CLI installed and running on your server.

Once you login with SSH to check if you have WP-CLI installed run the following command.

$ wp help

WP-CLI Installation

If you don’t have WP-CLI installed you can run the following sequence with commands in the terminal (assuming you have the permissions to that on your server).

$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
$ chmod +x wp-cli.phar
$ sudo mv wp-cli.phar /usr/local/bin/wp

If you don’t have the right permissions you can contact your server administrator (or support) and ask them to install it for you.

Note: The minimal requirements for WP-CLI are: PHP 5.3.2 & WordPress 3.4 or later.

WP-CLI is a very powerful tool that’s not really utilized as much as it should be. It has been integrated within WordPress since version 3.4. If you want to learn more about its capabilities you can visit the WP-Bullet blog that has many useful articles on this topic.

Subscribe to our Newsletter* if you find our content useful and would like to receive regular updates with the latest posts.

Quick Test

The quickest way to use the PHP script and generate your child theme files is to create a new empty PHP file and copy-paste the code from the GitHub gist.

The minimum to run the script is to pass the parent folder name using -parent option.

I am going to discuss how the script works and what are the additional options you can use in the next section. However, if you want to do a quick test just run the sequence with commands below.

$ cd public/wp-content/themes
$ wget -O boilerplate.php https://gist.githubusercontent.com/krasenslavov/bef457b8c0494d99f0d4ae30d6dd396b/raw/2bbedd1d169d54cffc81313ce76ba913fdee679d/boilerplate.php
$ wp eval-file boilerplate.php -child twentytwenty

This will make a copy of the PHP script hosted on GitHub inside the themes/ directory. Then generate and initialize child theme files for the TwentyTwenty default theme.

Here is the structure with files and folders for your newly created child theme.

twentytwenty-child/
  assets/
    css/
    js/
    img/
  footer.php
  functions.php
  header.php
  index.php
  screenshot.png
  style.css

The Options

You can additionally pass some options to the boilerplate.php script.

  • -name – Specify the name of your child theme.
  • -description – Specify the description of you your child theme
  • -screenshot – Add URL path from where we can generate a screenshot image of your child theme.
  • -bootstrap – It will load the latest version (4.3.1) of Bootstrap in the header and footer of your theme.

Note: If not specified, by the default, all of the above will be taken and generated based on the parent theme contents.

Here is an example of a full WP-CLI command including all the options:

$ wp eval-file boilerplate.php -child twentytwenty -name "2020 Child Theme" -description "This is my first child theme." -screenshot https://example.com/path/to/screenshot.png -bootstrap true
Use Wp-CLI and Create WordPress Child Theme Terminal

The Boilerplate

Lastly, I would like to give you some ideas about how to extend and customize the boilerplate PHP script.

In addition to child themes, you may want to use the same method to create a boilerplate for parent themes including all the files and directories (e.g. page.php, single.php, archives.php, etc.). In this case, I will put the file structure of my base theme in an array and then loop through it to create all the files and folders.

Also, you can additionally customize or extend the boilerplate.php file to meet your needs. For example, pass arguments with URIs or Author Name. Or validate and resize the custom child screenshot image to 1200×900 using the PHP ImageMagick library.

What’s Next?

As I mentioned before WP-CLI is a very powerful tool that comes up integrated into WordPress. There are many things you can do with it especially if you feel comfortable working in the UNIX terminal.

I will leave you with a couple more resources where you can see the full power of WP-CLI. The first one is from the official WordPress Handbook and the second one is an advanced article from Smashing Magazine.

And of course, the WP-Bullet by Mike Andreasen is the one I mentioned previously in this article.

If you have any questions and suggestions don’t hesitate to add them in the comments below.

‘Til the next one.

* We have a NO SPAM policy so you won't reaceive any meaningless emails from us!

Related Content

CPT UI + Advanced Custom Fields + Front-end Data Collection Form

How to use CPT UI and ACF to setup a front-end form and collect user generated content.

READ MORE...

Create Gutenberg Blocks with ACF 5.8+ PRO

How to register and customize Gutenberg blocks with the Advanced Custom Fields plugin (no JavaScript needed)

READ MORE...

Easy Custom User Role Feature Without a WordPress Plugin

How to add custom user roles to your theme without using a third party WordPress plugin.

READ MORE...

Leave a Reply

Your email address will not be published. Required fields are marked *

How to Use WP-CLI and Create WordPress Child Themes
Override Gutenberg Blocks & Create Gallery Modal Window

Thank you for reading our content. Use the buttons below to spread the love and share Easy Custom User Role Feature Without a WordPress Plugin with your followers.