How to use WP-CLI

Command line goodness

Are you down with the idea of getting more work done in less time? Maybe you like the idea of speeding up the workflow on your WordPress projects, but just don’t know where to begin.

Well you, my friend, have come to the right place.

What is WP-CLI? And how do you use it?

WP-CLI stands for WordPress Command Line Interface. It’s designed for you to give it exact commands and it will execute those commands inside WordPress, giving you the ability to manage your sites without a browser.

All right, this might sound cool in theory, but why in the world would we want to replace the WordPress Admin? It’s already super-easy to use, right?

Get a good whiff of your workflow

In programming we are constantly on the lookout for small indications of inefficient logic we call code smells. Simply put, a code smell is when you do something the long way when there is likely a shorter alternative.

While code smells aren’t technically “bugs,” they can lead to much bigger problems down the road if left unaddressed — especially at scale.

The same principle of code smells can also be applied to your project workflow.

 

  • Do you find yourself doing the same tasks over and over in your WordPress projects?
  • Is there a particular way you set up WordPress for each new project?
  • Is there a common problem you find yourself fixing for clients over and over?

Identifying areas of your project workflow that can be automated is the ultimate key to delivering consistency to each and every one of your clients. And with consistency comes the ability to scale your services without sacrificing quality.

Identifying areas of your project workflow that can be automated is the ultimate key to delivering consistency to each and every one of your clients.

Start making checklists to find things you can automate with WP-CLI.

One of the best ways to identify patterns in your workflow is to start making checklists of the things you find yourself doing for clients on a regular basis.

Ask yourself this: If I hired a virtual assistant today to handle my most mundane project tasks, how would I instruct them?

Checklists are a great starting point for discovering what you might be able to automate using tools like WP-CLI.

Your checklists can help you understand exactly what each task actually involves, step by step. They will help you think objectively about the work being performed, and you might be surprised how just the simple act of writing out steps will reveal things you know should be removed from your workflow.

How to use WP-CLI

WP-CLI is all about commands, so it’s important to understand what commands are available, and how they differ from one another. Luckily, there’s a definitive list.

The more you familiarize yourself with the available commands, the better. Going back over your workflow checklists, you will need to discover which of your tasks can be done faster with WP-CLI commands.

Here are some of my personal favorites:

This feels like as good a time as any to throw in a Spiderman quote:

“With great power there must also come great responsibility.”

Anatomy of a WP-CLI command

anatomy of wp-cli command

In WP-CLI, commands are made up of four parts: the package, the command, the subcommand, and options.

Package

wpThe package is always required. WP-CLI itself is the package, so every command will begin with the package call, and everything after it will be telling the package what to do.

Command

wppluginLikewise, the command is also always required. It represents the area of WordPress you want to manipulate. I like to think of commands as the major components that make up WordPress. It’s also worth mentioning that commands are always a singular noun.

Subcommand

wppluginactivateThe subcommand isn’t always required, but it usually is. They typically either represent a subset of the command’s major component or they are indicative of an action.

Occasionally, there can be more than one subcommand. As is the case with more complex components like WP-Cron.

wpcron

Options

wpuserget1I like to think of options as the specific details of my command and subcommand. But be careful, options aren’t exactly optional. In many cases you need at least one option to specify the object you want to target, such as a user ID.

Options that are optional have two dashes (–) before their name. It’s just an easy way to tell that those are the options you aren’t required to specify. You can also have a lot of options!

For instance, if we want to create a new user, there are two required options we must always provide: the username and email address.

wpusercreate2

But there are many other options we could optionally specify as well, such as role.

wprole

Just when you thought you knew everything about options, then you learn there are some options that don’t even require values! I like to think of options without values as flags that indicate a specific pre-defined behavior.

wppluginupdateA great example is updating all plugins. It would be a real pain if we had to list out every single plugin we wanted to update, so WP-CLI is smart and anticipates that a lot of people will probably just want to update everything and provided a flag for it.

What if I get stuck?

Not to worry, there’s a command for that, too!

If you don’t want to scour the online reference for details about what subcommands or options are available for a given command, simply use the help keyword followed by any command and/or subcommand.

WP-CLI will display a friendly guide, and when you’re done, just hit the Q key to close it.

wphelp

How can I get WP-CLI?

WP-CLI is a PHP archive, which is basically a way to package a bunch of PHP files into a single file. This means to get WP-CLI working all you need is one file! It’s very easy to install even for intermediate command line users. There are simple instructions right on the project home page.

The good news for beginners is that many Managed WordPress hosting providers these days pre-install WP-CLI on their servers for you. (Here’s how to use WP-CLI on a number of GoDaddy hosting plans, including Managed WordPress.) Check with your host to see if it’s already been installed and how to gain SSH (secure shell) access to your server.

WP-CLI on WordPress.tv

Want to learn more? Check out my recent chat about WP-CLI on WordPress.tv.

Image by: dbbent via Compfight

Frankie Jarrett
Frankie Jarrett is a full-stack developer from Missouri who has been building PHP web applications and WordPress products since 2007. He is Senior WordPress Product Engineer at GoDaddy and loves the fact that he gets to work with WordPress every single day. From launching his own theme shop in 2011, to building large-scale enterprise websites, authoring dozens of plugins, and now creating amazing product experiences on GoDaddy's managed hosting platform — WordPress has always been at the center of Frankie's career, and he plans to keep it that way!