As a developer, I find I do a lot of the same things on a regular basis. I may set up a new plugin, start a new theme, add a specific feature to a theme or plugin, set up tests, or other repetitive tasks. These tasks usually share a similar starting point, even if the end result is different. Scaffolding for WP CLI makes it easy to jump start projects by quickly performing some of these repetitive tasks.
For example, a plugin will usually have certain files and folders like:
- readme.md (or .txt)
Almost every plugin I build starts like that. I’ve even made a plugin template so I can build those out, then go in and change a few file names before starting to build the plugin. It saves a good bit of time, but it’s also something that requires finding my template, copying the template, and editing certain files in the template so it’s ready to use.
That’s faster than typing all of those things out manually, but the process can be simplified even more.
Before I started full time development I worked in construction and set up scaffolds on a regular basis. If you walk down a typical city street, you’re bound to eventually walk under a scaffold. These are the metal platform structures that workers are able to climb up and then walk across in order to access tall buildings.
Scaffolds are also used in other industries. They are a framework to enable building and are amazing tools for jump starting many different things. In WordPress development scaffolds are the perfect tools to jump start projects or components.
The first step to working with scaffolding in WordPress is to install the WordPress Command-Line Interface (CLI). This is a tool that will let you run WordPress commands and some additional custom commands from a command line.
One advantage of WP CLI is the built in
scaffold command. This command, and the sub-commands used with it, will allow a developer to rapidly add components to a site.
Built-in commands include:
|wp scaffold child-theme||Generate child theme based on an existing theme.|
|wp scaffold plugin||Generate starter code for a plugin.|
|wp scaffold plugin-tests||Generate files needed for running PHPUnit tests in a plugin.|
|wp scaffold post-type||Generate PHP code for registering a custom post type.|
|wp scaffold taxonomy||Generate PHP code for registering a custom taxonomy.|
|wp scaffold theme-tests||Generate files needed for running PHPUnit tests in a theme.|
|wp scaffold _s||Generate starter code for a theme based on _s.|
This is enough to jump start most projects.
It’s also possible to add additional scaffold commands. For example, if you are getting started with creating CLI packages, then you may want to add the
scaffold package command to your CLI instance, which will give you a scaffold for building packages. Since you can use this to build more scaffolds it becomes a bit like scaffold inception.
The real power of scaffolding comes in with the ability to create custom scaffolds. Let’s look at the
wp scaffold plugin output.
wp scaffold plugin --prompt 1/12 <slug>: my-plugin 2/12 [--dir=<dirname>]: 3/12 [--plugin_name=<title>]: my plugin 4/12 [--plugin_description=<description>]: this is my plugin 5/12 [--plugin_author=<author>]: Nick Croft 6/12 [--plugin_author_uri=<url>]: https://reaktiv.co 7/12 [--plugin_uri=<url>]: https://github.com/reaktivstudios/my-plugin 8/12 [--skip-tests] (Y/n): n 9/12 [--ci=<provider>]: 10/12 [--activate] (Y/n): n 11/12 [--activate-network] (Y/n): n 12/12 [--force] (Y/n): n Success: Created plugin files. Success: Created test files.
Cool Tip: Use
--prompt with the WP CLI command and you will be prompted for the available arguments. Easier than having to remember and type them all out.
That doesn’t really look like the structure that I use for my plugins though. It’s missing the assets and inc folder, which means I need to still copy those in.
However, if I use the
package command to make my own package and use the example of the
package command for building the package, I can make a custom plugin scaffold that will build the structure the way I like it to work. To make getting started easy, I created a sample package that’s available in GitHub.
The command and output are very similar:
wp scaffold custom_plugin --prompt 1/12 <slug>: my-plugin-2 2/12 [--dir=<dirname>]: 3/12 [--plugin_name=<title>]: My Second Plugin 4/12 [--plugin_description=<description>]: An example scaffold plugin 5/12 [--plugin_author=<author>]: Nick Croft 6/12 [--plugin_author_uri=<url>]: https://reaktiv.co 7/12 [--plugin_uri=<url>]: https://github.com/reaktivstudios/my-plugin 8/12 [--skip-tests] (Y/n): n 9/12 [--ci=<provider>]: 10/12 [--activate] (Y/n): n 11/12 [--activate-network] (Y/n): n 12/12 [--force] (Y/n): n Success: Created plugin files. Success: Created test files.
Of course, this is a very simple example of extending the scaffold. From here you might want to fork my sample repo to make your own custom plugin scaffold. Some good ideas include:
- PHPCS rules
- Custom Tests
- CI integration
- Gulp support and structures
It isn’t limited to plugins either. We are using the scaffold at Reaktiv to make it easier to add components to projects including:
- Customizer interface
- Other CLI commands
We have plans for other things we do regularly that we want to standardize. This makes it easier and faster to add new features and makes the code more standard. That is a big win for our clients because work is done more quickly with better quality, and any developer can step into a project and know how it is organized. With scaffolding, projects are easier to work on and maintain across the board.