What Is the WordPress CLI (And How Can You Use It)?
In this guide, we’ll introduce to you the WordPress CLI. We’ll also show you how to install it on your WordPress website and illustrate how you can use it.
As you get more comfortable developing for WordPress, you’ll want some tools that will help you complete your tasks quickly and efficiently. The WordPress admin dashboard is intuitive and comprehensive, but it can also be time-consuming to navigate.
What if you want to manage your site more directly, with just a few easy commands?
This is where the WordPress Command Line Interface (WP-CLI) comes in handy. The WP-CLI is a tool that enables you to interact with your WordPress site directly by using commands in a text-based interface. It’s also very comprehensive, featuring a wide variety of potential commands. Almost anything you can do on the back end of your site, you can do much faster using the WP-CLI.
An Introduction to the WordPress Command Line Interface (WP-CLI)
Like most WordPress users, you’re probably very familiar with the WordPress admin area. It works well, but it’s not the only option for managing your site. In fact, it’s not even the most direct or efficient way of doing so. Having a graphical interface that you can navigate is certainly preferable for some users. However, it does mean you’ll spend a lot of time navigating through menus or waiting for pages to load.
Enter the WordPress Command Line Interface (WP-CLI).
As the name suggests, this tool enables you to perform administrative tasks on your WordPress site using a command line. Through this method, completing a task only requires the time it takes to type in a line of code and hit Enter.
The beauty of the WP-CLI is how it gives you direct control over your site. Anything you can do in the WordPress admin dashboard, you can do using the WP-CLI instead. It’s not nearly as complicated as you may fear, and there are plenty of resources available if you want to learn more about it.
To use the WP-CLI, you’ll need to install it on your WordPress site. Let’s look at this process in more detail now.
How to Install the WP-CLI on Your WordPress Website
If your site is hosted with DreamHost, it will already have the WP-CLI installed. If you need to, however, you can also install this tool manually.
However, you’ll first need to make sure that your environment is compatible, meaning that it conforms to the following specifications:
- A UNIX-like environment (OS X, Linux, FreeBSD, Cygwin)
- PHP 5.3.29 or later
- WordPress 3.7 or later
That first point might be a problem for some users. The WP-CLI is made with UNIX-like environments in mind and has limited support for Windows. It is still possible to install it on Windows, but beware that it may require some additional tinkering.
To install the WP-CLI in one of the environments on the above list, you’ll need to use Secure Shell (SSH) to download and configure the necessary files. So you’ll want to make sure that’s up and running. First, you need to download the wp-cli.phar file to your root directory, using the following command:
You should then check to make sure that the file is working, using another command:
Next, you’ll want to make the file executable, which will enable you to use the wp command. You’ll also want to move it to another directory. This final command will perform both tasks:
With that, the WP-CLI should now be successfully installed. You can test it by running the command wp --info. If everything works, you’ll see information about your version of the WP-CLI displayed.
That’s it! You’re now ready to use this tool to manage your site more efficiently. Before we move on, however, let’s run through some alternative methods of installation.
Alternative Ways of Installing the WP-CLI
As we alluded to earlier, there are actually several methods of installing the WP-CLI. We won’t detail all of them in this article. However, we’ll list them out briefly and link to more information on each, so you can choose the technique that best suits your needs.
You can use the following tools to install the WP-CLI on your site:
You might also be interested in installing the ‘nightly’ version of the WP-CLI (its most recent pre-release build). Nightly should be more or less stable to use in a development environment, but you should be aware that it’s not yet officially released. Finally, if you want to get involved in developing for the WP-CLI yourself, you can do so by setting it up with Git.
5 Ways to Use the WP-CLI to Manage Your WordPress Site
The WP-CLI provides total access to your site — right at your fingertips. There are dozens of commands you can use to manage nearly every aspect, and that’s not even taking into account custom commands.
We’re now going to look at just a few of the standard commands available to you. This is to give you a taste for how you can use WP-CLI to manage your site before you dig deeper into the rabbit hole of possibilities.
1. Install and Update WordPress
The most fundamental task you can accomplish with the WP-CLI is to download and install WordPress on your site. The command for downloading WordPress is simply:
This will download and extract WordPress in the current directory. You can also add additional parameters to refine the download further. For example, the locale parameter determines which translation of WordPress will be used. This command will download the Brazilian Portuguese version of WordPress:
Once downloaded, you can install WordPress using the install command. This command contains a number of parameters that configure the setup. Let’s take a look at an example:
As you can see, this is all fairly self-explanatory. Simply replace the example data in each parameter with your own values. To ensure that everything has worked as expected, you can use the following command to test the installation:
This will return the version number of your installation, proving that WordPress has been successfully installed! Now you just need to know how to make sure it’s updated, which you can do with the following command:
If a newer version of WordPress is available, it will be downloaded and installed automatically when you run this command.
2. Manage Themes and Plugins
There are many ways you can manage themes and plugins using the WP-CLI, so let’s look at some of the basic options now. First, you can use the list command to view a list of your themes or plugins. This can be filtered using parameters to only display items with a specific status (such as inactive) or to return the results in a particular output format.
For example, if you want to list all inactive themes as a CSV list, you can use the following command:
You can also install a plugin by specifying its slug in the plugin directory, providing the path to a local file, or entering the URL for an external file. In this example, we’re also going to activate the plugin at the same time:
It’s also easy to change the status of a plugin or theme. This command can be used to enable a theme, which in this example is Twenty Seventeen:
There’s also a command for deactivating a plugin, which we’re using here to disable the Hello Dolly plugin. In this example, the command is also configured to uninstall the plugin:
Finally, you can search the respective directories looking for a specific plugin or theme. For instance, let’s search for a theme containing the string “photo”. We’re also setting it to return three results instead of the default ten:
This will display the following table:
As we mentioned, this is only a small taste of how you can manage themes and plugins using the WP-CLI. Hopefully, you’re getting a sense for how useful this tool can be.
3. Create a Child Theme
By using the scaffold command, you can generate a child theme that includes the functions.php and style.css files. We recommend that you do this if you want to make changes to an existing theme.
All you need to do is to specify the slug for the new child theme, and for the theme you’re using as the ‘parent.’ In this example, we’re creating a child based on the Twenty Seventeen theme, and we’re giving it the slug twentyseventeen-child:
If the process is successful, you’ll see a message that the child theme has been created, along with the path to its directory:
You’ll now find the child theme in the specified template, ready to be edited!
4. Moderate Comments
Moderating and managing comments is made a lot easier in the WP-CLI, which enables you to quickly create, delete, and edit them. There are many comment subcommands available, but let’s look at some of the most basic options.
First, you can add a new comment. The following command will add a comment to a post with the ID of 20, and specifies the contents and author:
Before you manage existing comments, it can be helpful to get a current list. You can do this with the list command, and the results can be filtered in multiple ways. For example, using this command will return a table containing the comment ID and author name for all approved comments on the post with an ID of 3:
This is what the resulting table will look like:
If you want to delete comments, you can do that by specifying the comment IDs individually, like this:
You can also delete multiple comments by separating each ID with a space. In this example, we’re also using the force parameter, which permanently deletes comments instead of adding them to the trash bin:
With a little practice, you can work through your site’s comments very quickly using WP-CLI commands.
5. Update the WP-CLI
As with every aspect of WordPress, you should always make sure that the WP-CLI is up-to-date. Fortunately, this is very simple. All you need to do is run the following command:
If your version is the most recent available, you will get a message confirming this. However, if a new version can be downloaded, you’ll be prompted to accept the installation. If you select yes, the WP-CLI will be updated, and you’ll see a confirmation message:
With that, you’ve updated your installation of the WP-CLI.
By now, you’re beginning to see what you accomplish using this simple interface. There’s more to learn, but you should be proud of how far you’ve come already!
Conclusion
Speed, accessibility, and efficiency are all traits that any smart developer looks for in their tools. The WP-CLI offers all of these and more while enabling you to manage your WordPress site remotely. Using the WP-CLI lets you do anything you can do in the WordPress admin — just much more quickly (once you’ve had a bit of practice).