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.
🎉 email@example.com:~/$ 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.
There are two things that you need to have to execute the PHP script, generate and initialize a child theme structure.
Once you login with SSH to check if you have WP-CLI installed run the following command.
$ wp help
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.
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
You can additionally pass some options to the boilerplate.php script.
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
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.
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!