Categories
Advanced Tutorials

Create WordPress Child Themes with 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 child theme start files with a single WP-CLI command.

Codeable.io

In this quick tutorial, I will share a PHP script I have put together that will allow you to extend parent themes and generate child theme start files with a single WP-CLI command. You can use any existing theme 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

You need to have two things to execute the PHP script and 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 log in with SSH, run the following command to check if you have WP-CLI installed.

$ 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 permissions 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 potent 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.

Quick Test

The quickest way to use the PHP script and generate your child’s 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 will discuss how the script works and the additional options you can use in the next section. However, if you want to do a quick test, just run the sequence with the 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 copy the PHP script hosted on GitHub inside the themes/ directory. Then generate and initialize child theme files for the TwentyTwenty default theme.

The structure with files and folders for your newly created child theme is here.

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 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

The Boilerplate

Lastly, I would like to give you some ideas about extending and customizing 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.

What’s Next?

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

I will leave you with a couple more resources to 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.

Leave a Reply

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