Mac Command, by Hannah Joshua

Earlier this month I shared a post on how to install, configure, and use Docker in local WordPress development. The main advantage of Docker was its containers: each WordPress installation was encapsulated and isolated from the others. These containers were started by using a configuration file and a simple command: docker-compose up -d.

The first time you started a Docker container with WordPress, you had to go through the WordPress installation process:

Installing WordPress on a Docker Container
Installing WordPress on a Docker Container.

After writing that post, I thought it’d be great to complement it with another tool that helps us to manage WordPress installations from the command line itself. Well, wait no more! Today I’m going to teach you how to use WP-CLI to manage WordPress installations from the console.

What is WP-CLI?

The command line is every developer’s best friend. WP-CLI (WordPress Command Line Interface) brings the management and maintenance of WordPress to our command line and it’s an indispensable component for any developer who wants to seriously optimize their time. With WP-CLI you will be able to update plugins, configure multi-site installations, and many other things without having to use a web browser.

Installing WP-CLI

The project documentation explains how to install WP-CLI. There are many options to do it, but the easiest one is to download the executable file directly:

and launch it using PHP:

If we want to use the command in a more “friendly” way, we can give it execution permissions and move it to some directory that is in our $PATH:

and from then on we can run the command as follows:

By the way, remember to run wp from the WordPress root directory:

WP-CLI and Docker

Suppose you are interested in using WP-CLI to manage the different projects you work with. If you followed my guide a few weeks ago, you’re probably using Docker now. So the question is: how can you use WP-CLI and Docker together? Is it possible to run WP-CLI in your WordPress Docker containers?

Ideally, the WordPress image we use in Docker should include WP-CLI. Unfortunately, at the time of writing this post the official image doesn’t include WP-CLI (and adding it can be tricky). But don’t worry! There are other images we can use that do include WP-CLI out of the box. Instead of using this:

use the following image:

And that’s it! Running docker-compose up -d will download a new WordPress image that includes the WP-CLI binary.

The only problem we have now is that WP-CLI is inside the container (i.e. the “guest machine”), and we are using our terminal on the “host machine”. How do we access it?

To execute a guest command from our host we must use docker-compose as follows:

For example, suppose I’m working on our Nelio Content plugin, which I have in a directory called nelio-content. In the docker-compose.yml file I created for this project I defined two services: one for WordPress (which I called wordpress) and the other for the MySQL database (mysql). Well, since WP-CLI is in the WordPress container, this is how we run it:

Finally, if you want to use a friendlier version, create the following alias:

and you’ll be able to run the command using just two simple letters:

WP-CLI Commands

Now that we have WP-CLI installed and know how to invoke it, it’s time to see some examples of what it allows us to do.

What WP-CLI Commands Look Like

In general, WP-CLI commands follow the following pattern:

In the documentation you have information about all available commands, their parameters, and how to use them. Don’t forget to have it at hand to consult it when you need it.

WordPress Management

Installing WordPress…

As I advanced in the introduction, the first thing you have to do when you start a new Docker container with WordPress is to complete the WordPress installation process. With WP-CLI, this is as easy as executing the following command:

Updating WordPress…

If you want to upgrade to the latest version of WordPress, just run this:

And if we want to go back to an older version, we can do so with the following command:

Plugin Management

Another common task we face when working with WordPress is plugin management. For example, right after we started a new Docker image with WordPress, we’ll see that said image includes multiple plugins we might not be interested in. How do we know which plugins are installed? How the we get rid of them? How do we activate the one(s) we want?

Listing Installed Plugins…

If you want to see the plugins installed in your site, run the list subcommand of the plugin command:

In my case, this returns:

Deleting Plugins…

To delete plugins you no longer want, just call wp plugin delete and specify the plugins you want to delete:

Activating Plugins…

And to activate a plugin, repeat the process, but use the activate subcommand instead:

Installing Plugins…

To install a new plugin, run the following:

and WordPress will download the plugin from the plugin directory on WordPress.org and will activate it automatically. Easy, isn’t it?

Theme Management

Theme management is very similar to plugin management: we can list installed themes, switch the active them, delete them, etc. For example, to list the available themes we have the following command:

which gives us the following result:

In this example, the currently active theme is twentyseventeen. How would you switch it to twentynineteen? It’s super easy:

What if we want to update all the themes that have available updates? It’s super easy too:

How cool is that? ?

And Now, What?

This has been a brief introduction to the use of WP-CLI. If you want to delve deeper into the possibilities it offers, in WordPress.org you will find all the documentation about this tool: how to install it, how to configure it, and how to use it. I recommend you read the documentation thoroughly and continue to discover the potential of WP-CLI on your own.

Featured Image by Hannah Joshua on Unsplash.

Leave a Reply

Your email address will not be published. Required fields are marked *

I have read and agree to the Nelio Software Privacy Policy

Your personal data will be located on SiteGround and will be treated by Nelio Software with the sole purpose of publishing this comment here. The legitimation is carried out through your express consent. Contact us to access, rectify, limit, or delete your data.