Thursday, March 16, 2017

Why WordPress Is Excited About the WP-CLI

The WordPress REST API gets lots of attention, but for many of the same reasons, the WordPress command line interface — WP-CLI — is also worthy of this type of excitement. A REST API and a CLI are essential to modernizing WordPress.

Like the REST API, WP-CLI is an alternative way of manipulating data on a WordPress site. Using simple commands you can change a user's password, bulk import posts, activate a theme, and so much more.

The WP-CLI is a community project that is led by Daniel Bachhuber. It was recently announced that the project would become an official part of the WordPress open source project with financial support to continue its development.

In this article, I will explain why WP-CLI is important. How to install it, show you some examples of how to use it, and give you some resources to learn more.

Why WP-CLI?

Not everyone needs or should use WP-CLI. Command line tools are not for everyone. Personally, I use it in my local development environments because it saves time setting things up and to deploy scripts.

Having WP-CLI installed on my servers has saved me more than once. Like when I needed to reset a user password on a site that was having email issues.

If you are comfortable operating your computer and/or your remote server — via SSH — via the command line, then you really should look at WP-CLI. If not, well maybe you should use WP-CLI as an opportunity to get more comfortable with the command line. WordPress is, after all, an excellent gateway to new technologies.

If you often need to create dummy content on a test site, do big content imports, make changes to a site every time you do a deploy, or manage a lot of sites then you really should take a look at WP-CLI.

What Exactly Is WP-CLII?

Like the WordPress REST API, WP-CLI lets you interact with your content. It uses the same internal functions as the WordPress UI to access or modify that data. For example, the REST API post routes & the WP-CLI post command still use standard WordPress PHP APIs — WP_Query, wp_insert_post, etc — to interact with post data.

The REST API and WP-CLI are both alternative interfaces for WordPress content and settings. They are both highly extensible. There is a growing list of additional packages for WP-CLI to provide additional functionality or control plugins via  WP-CLI.There is even a package, RESTful WP CLI that turns all REST API routes into a WP CLI command. You can find out more here.

Installing WP-CLI

Are you ready to try out WP-CLI? First, you will need to install it. I recommend starting in your local development environment. Most WordPress-specific development environments already include WP-CLI, for example, it is pre-installed on VVV. If you are using DesktopServer, make sure to check the "DS-CLI" option when starting or restarting the server. This makes the WP-CLI available to all of your DesktopServer sites and gives you additional powers available via the DesktopServer CLI — DS CLI. If you are using DesktopServer blueprints, you can automate site setup, via DS CLI when creating new sites.

If you don't have WP-CLI already or are installing on your server, it's pretty easy to do. Open the terminal on your computer or SSH into your server. Then you can run these three commands:

Once that is done, try the command wp –info. This should give you basic information about WP-CLI, and confirms it works.

Some Basic Commands

Now thatWP-CLII is installed, let's try a few things. Start by switching to a directory with WordPress installed.

Let's create a post. You can use the wp post create command for this:

This will show you the ID of the new post. If you want to delete it, you can do that with wp post delete. Here is an example assuming your new post has the ID 42:

What if you wanted to test the performance of your server. How will it handle 50,000 posts and 10,000 terms?

Help With Imports and Deployment From WP-CLI

If you want to do a more complex import, you can prepare a CSV file and import it using the wp cli import csv command. This is an example of a custom package. Custom packages are like plugins for WP-CLI. Like plugins, they have to be installed separately.

All packages listed on the packages list, can be installed directly through WP-CLI. For example, there is a cool package for creating login links that don't require passwords. You can install it like this:

Many plugins provide add-on for WP-CLI commands. For example, Delicious Brains' excellent plugin Migrate DB Pro has a WP-CLI plugin. I recently had a site under development where on each deploy of code to the staging site, I needed to pull the live site's database. But after I did this, the site's front-end would white screen since the staging site's code didn't include the live site's theme, since I was working on a new theme.

In my deploy script I added a command to pull the live site database, using Migrate DB Pro, and then once that was done, to switch themes. It looked like this:

Want To Learn More?

I hope this article motivated you to try out WP-CLI. Once you experience its power, I think you will be hooked. The documentation on the official site is really quite good. I will leave you with a great video from WordPress.tv where Shawn Hooper will show you more about managing WordPress from the command line.

Josh Pollock

Josh is a WordPress developer and educator. He is Founder/ Lead Developer/ Space Astronaut Grade 3 for Caldera Labs, makers of awesome WordPress tools including Caldera Forms — a drag and drop, responsive WordPress form builder. He teaches WordPress development at Caldera Learn.


Source: Why WordPress Is Excited About the WP-CLI

No comments:

Post a Comment