The WP-CLI is awesome. It allows you to automate a lot of the tedious development practices within WordPress. It’s right up there at the top of my “To Learn” list. Just behind Gutenberg development, of course.
What is the WP-CLI
The WP-CLI gives you a command-line interface for performing WordPress admin tasks. It’s basically super powers for administrating a WordPress site.
You can update plugins, install themes, delete users, and more. Like creating a script that downloads WordPress, creates a database, installs core, adds plugins, activates your theme, and imports demo content. All from your command line. 🤯
Where to Run Commands
You will run commands from an application like Terminal. Don’t let that deter you though. You can get a lot of power by learning just a few commands.
In Terminal you’ll need to be within the root directory for your WordPress site or the directory soon to be your WordPress site. If you’re running the WP-CLI for local development you can run
If you’re running the WP-CLI on a server you’ll want to get SSH into your server. Typically something like
ssh [email protected] will get you in. But check with your host for SSH access.
How to Install the WP-CLI
I’m not going to spend time breaking down the full installation process here. Other have done great work covering all kinds of use cases.
- If you use Vagrant, VVV comes with WP-CLI installed
- If you use MAMP Pro, check out this article by Tom McFarlin
- WP Local Docker by 10up comes with WP-CLI tools too
- I’ve heard great things about Local by Flywheel
Installing on a Server (or other local environment)
WP-CLI Stuff to Try
Now that you’ve got the WP-CLI installed it’s time to try some stuff. Break some stuff. Fix it. And repeat. Here’s a few ideas to get you started.
Search and Replace that Database to Upgrade to HTTPS
You know what isn’t fun? Updating a database to change
https://domain.com. Unless you’re using the WP-CLI that is.
wp search-replace command makes this easy, and safe.
wp search-replace ‘http://domain.com' ‘https://domain.com' --dry-run will do a test run and let you know how many items will be replaced.
--dry-run flag and run the command again to make it official.
Want to be extra cautious? Make use of the
wp export command to export a database and if you need to revert the handy
wp import will let you do just that.
Generate some Dummy Posts
Have a WordPress site you want to fill up with some dummy content. The WP-CLI can do that too.
wp post generate --count=10 and you’ll have 10 posts full of great dummy content.
Fun Fact: This post was created with the
wp post create command.
Updating plugins is a PITA. Especially if you’re managing multiple sites. With the WP-CLI you can easily update plugins from the command line with
wp plugin update --all.
Pro tip: Set up some scripting to run one script and update plugins across multiple sites on multiple servers!
Create a Child Theme
Creating child themes is no fun. I don’t do it enough to have it exactly committed to memory. And then I’ve got to double-check the parent theme slug and CSS handle.
Instead just drop this in the command line and boom. You’ve got a child theme.
wp scaffold child-theme sample-theme --parent_theme=twentysixteen
wp scaffold commands give a full set of options for generating plugins, child themes, and even a starter theme based on _s. Neat!
Wrapping Up and Where To Go
Those are some really simple WP-CLI commands that can save a ton of time in development. And there are a lot more options. Take a look at all of the available WP-CLI commands.
The real power of the WP-CLI comes with scripting to make a single command run multiple commands. There’s also a ton of packages made available giving the WP-CLI even more functionality.
So get the WP-CLI installed and start saving time by automating your workflow.