Speed Up How You Manage WordPress With WP-CLI and the Command Line
If there’s one thing WordPress developers hate, it’s plodding through unnecessary manual grunt work that could just as easily be automated.
And if there’s one thing developers love, it’s using the power of the command line to blast through tasks with just a few keystrokes without disturbing the overall flow.
WordPress hasn’t exactly been blessed with a huge range of power tools for developers over the years, but that situation is changing rapidly. WP-CLI is quickly becoming one of the key components of every serious WordPress developer’s toolkit.
A command-line interface for WordPress, WP-CLI takes its inspiration from Drupal’s Console site and offers a similarly convenient way of quickly executing common tasks.
In this article, we’ll step through some of WP-CLI’s core functionality and outline why you should be adding it to your set of solutions.
An Overview of WP-CLI
If you’re looking for a handy summary of what WP-CLI is all about then look no further than the description on the project’s homepage:
WP-CLI is a set of command-line tools for managing WordPress installations. You can update plugins, set up multisite installs and much more, without using a web browser.
Andreas Creten provided an excellent overview of the history of WP-CLI at WordCamp Praha 2015 where he outlined its development path to date:
Common use cases for WP-CLI include creating new sites, managing plugins, populating sites with test content and flushing caches. Whether it’s in the context of a single local development site or a live multi-site install, they’re precisely the sort of tasks that scream out for clever tooling and possible automation.
An increasing number of hosting companies are now also pre-installing WP-CLI in live environments, so it’s very much in your interests to review at least the basics of what it can do.
The commands available in WP-CLI are divided into two classes:
- Internal commands: The core list of WP-CLI functionality.
- Community commands: Commands created by third-party developers.
The second set of commands highlights one of WP-CLI’s most attractive features: the ability to define custom commands. Adding your own command is as simple as writing your own PHP class. There are clear instructions to get going with this over at the project’s Github page in the Commands Cookbook section.
Even a quick scan through the list of community commands shows the range of day-to-day WordPress uses that have already been tackled here such as Nginx caching, Jetpack, sitemap generation, cron jobs and many more.
Getting Up and Running with WP-CLI
In terms of requirements, WP-CLI is relatively straightforward. You’ll need PHP 5.3.2 or later, a UNIX environment of some description and WordPress 3.5.2 or later. It’s also a major help if you’re already familiar with the basics of the command line and navigating a UNIX system.
WP-CLI can be installed in a variety of ways. Downloading the Phar file is the recommend method highlighted on the homepage for most users and further options for installing via Composer and Homebrew are outlined on the project’s Github page. Everyone’s setup will naturally vary so, rather than step through the various options for install here in great detail, we recommend consulting the appropriate documentation and going from there.
In my case, an initial install attempt via Homebrew on a Mac opened up a can of local Ruby-related worms so I ended up going for the curl option. This required a bit of further tweaking to .bash_profile to get everything playing nicely with MAMP.
To make a long story short, as with any type of software install, you may well have to consult our old friend Google at some stage for assistance depending on the vagaries of your setup.
This installation overview from Kailey Lampert gives a good summary of various steps to help you along the way, however, and addresses several possible pitfalls. There is also a handy video overview:
Once you have your install completed, pop into a WordPress root folder via the command line and type
wp --info. You should be seeing something along the following lines:
If you’ve gotten this far, you’re good to go.
Some Notes on Help and Configuration
As befits a command-line tool, WP-CLI ships with detailed built-in support and documentation. You can see a full list of available commands at any time by typing
wp help or simply
To access detailed help on a particular command – for example, the database export command – simply type
wp help followed by the name of the command itself:
This should kick you into a familiar Unix man page type environment as shown below:
If you’re operating in a multisite environment, you pass specific URL parameters as flags to select the site you’re after. One handy point to note here is that these types of global parameters can also be stored in a YAML config file per site for convenience.
Right, with the software humming along in the background, let’s turn our attention to some commonly used pieces of functionality.
Using WP-CLI to Update WordPress
Updating WordPress has improved immensely as a process over the years, but it’s still something of an administrative chore – especially if you’re maintaining a number of sites. It’s a great place to start however in showing the convenience and power WP-CLI can bring to your workflow.
We’ll begin by checking the current version of WordPress in our active install:
In my case, it seems like we’re still running WordPress 4.1.2, so it’s definitely time for an upgrade.
Naturally, we’ll want to back up the database before going any further and doing that is just a command away.
The terminal seems to show success in exporting:
And a quick trip into our root folder shows the exported database.
Because I didn’t specify a filename for the export via a flag, it pops out by default with the name of the host folder. Running an upgrade is now as simple as entering the following:
And now, with just that simple command, our install is updated:
Naturally, you’ll want to give the site a quick once over visually before going any further but, assuming everything is in order, you’re now free to delete or archive your backup .sql file.
In the interests of full disclosure: the entire sequence above was not quite as hassle-free as indicated, as I did run into some trouble with the export command not being able to find mysqldump on a MAMP install at first.
It’s worth bearing in mind – as you begin to explore the power of WP-CLI – that you may well run into your own technical snafus along the way. Stay patient and remember that Google and Stack Overflow are your friends!
By exporting a database and updating core files we’ve shown that WP-CLI enables us to make some pretty major changes with just a few keystrokes. Let’s briefly cover creating a site from scratch.
Creating New WordPress Sites
We’ll start the process of creating a new WordPress site locally by making a folder to house it and navigating into that folder.
From within the new folder we now download the latest version of WordPress.
As you can see in my example, by omitting the version flag, the latest version is automatically downloaded.
Now we need to set up the wp-config.php file with some info about our database:
And then finally we need to actually install by running
wp core install and passing in some relevant parameters:
And with that, our site is installed.
As you can see from the screenshot above, I hit a slight bump in the road by forgetting to actually create the database, but that was swiftly remedied.
A quick bit of tinkering with the local hosts file and we are in a position to see the fruits of our minimal labor.
It may seem like a slightly involved process the first time around but, once you have your workflow dialed in, you are looking at potentially exponential improvements to many common tasks in terms of speed.
Let’s very briefly touch on plugins and themes before we wrap up.
Managing Plugins and Themes
With our new site installed, full control over plugins and themes is just a few keystrokes away. Let’s start by checking the currently active theme:
As you’d expect on a fresh install, the only items listed are default WordPress themes, and the currently active one is highlighted.
Our last item to check is the plugin situation. As before, we can see what’s currently installed nicely and simply:
Not much happening so far on a standard install of course, but it does pick up what’s installed and what needs updating as we can see:
I have total control over installing new plugins, updating across the board and deleting what’s no longer necessary via WP-CLI’s full set of plugin commands.
WP-CLI shows all the signs of being around for quite some time to come, and we’ve only scratched the surface of the options available in this overview. Consult the following resources for further in-depth info:
- The WP-CLI homepage: Your first port of call should undoubtedly be the project homepage itself where a full list of core info, config options and community commands are available.
- Tim Nash Video Tutorials: UK developer Tim Nash (Go England!) has put together a comprehensive series of WP-CLI tutorials over on his site. It’s a great jumping off point for getting into more advanced use of the software.
- Other articles on WPMU DEV: We’ve also explored WP-CLI a little here on the site in the past. Daniel Pataki’s WordPress Developer Tools breakdown makes a great starting-off point, and his guide to Vagrant and WordPress kicks things up a notch from there.
As WordPress continues to grow in terms of its power and flexibility, it’s more and more important for developers to be making the most of any tools available to keep their workloads manageable and their workflow smooth.
With its comprehensive range of functionality and the crucial ability to be able to extend that functionality to match your needs, WP-CLI is a tool that has already earned its place in many WordPress developers’ toolboxes.
We’re curious to hear if you’re already making use of WP-CLI or if there are aspects of its functionality that you’d like to see us explore in more detail here on the site in the future. Get in touch in the comments below and share your thoughts.Tags: