WP-CLI: A Beginner’s Guide

Anyone new to WordPress can get easily overwhelmed by just the complexity of its interface. After all, as the CMS that 39.9% of all websites use, it needs to be pretty feature-rich. It typically takes weeks and a lot of caffeine to learn the ins and outs of the Graphical User Interface (GUI). Though knowing the WordPress GUI is a skill in high demand, it’s only part of being a true guru!

It’s easiest to think of WordPress like you think of operating systems like Windows, Linux, and Mac. While you could just use the top layer and leave it at that, you’d only be using a fraction of its power. If you’ve scripted for Linux before, I would like to apologize for that trauma. However, you can rest assured that WP-CLI is far more straightforward and easy to learn.

The reason that this knowledge is in such high market demand is because many people assume it’ll be impossible to learn. Even the verb “script” sounds pretty intimidating! In truth, it’s one of the easiest ways to make money online. If you are already a WordPress admin, it can greatly shorten the amount of time you spend doing mundane tasks.

Without further adieu, let’s dive into how we can harness the power of the WordPress CLI!

What is WP-CLI?

Before you become the world’s next top WordPress CLI developer, you’ll need to know what it is. The name is simply the acronym for “WordPress” mashed together with an old-time acronym for “Command Line Interface”. A CLI is the opposite of a GUI; everything runs in what’s called a “terminal”.

Unlike your GUI, where you can click buttons and visually inspect what’s happening, everything with WP-CLI happens behind the scenes. Though you can run commands and scripts that have powerful site-wide impacts, the CLI itself doesn’t usually give much feedback. That’s why it’s critical to use a well-trusted WordPress backup plugin before you start out using WP-CLI.

It’s quite easy to enter a command, though cleaning up an erroneous command can be a completely different story. As the adage goes, it’s always easier to destroy than to create! At this point, you might be wondering why you should even care about WP-CLI.

Many think “they didn’t even bother to make a GUI for it, so it can’t be too important”. As a matter of fact, the power of it lies in it being text-based. This makes it far easier to automate tasks. Once you become proficient with it, you’ll probably wonder why you didn’t switch sooner!

WordPress Command Line Explained

So, how does WP-CLI really work? Simply put, it’s the backbone of WordPress. Every time you click anything within the CMS, it’s actually WP-CLI doing the heavy lifting. It’s a small, multi-platform program. However, since the vast majority of web servers are Linux, you’ll most likely be running it through the Linux terminal.

Technically, you could complete an entire WordPress installation on a new server with WP-CLI. You can also create .sh files that are sequences of commands. This lets you share your creations easily with the rest of the WordPress world.

WP-CLI can help administrators majorly cut down on unnecessary plug-ins that could be slowing their sites down. After all, the vast majority of plug-ins are essentially a stylesheet and a .sh file with a series of prompts and commands. We’ll go over how these work in detail a bit later.

Using WP-CLI to Install Themes

Sure, it’s easy enough to just use the GUI to install themes. However, if time is of the essence, a one-line command will also do the trick. Let’s say that you have a theme on your server called “geocities”. You want to apply this theme to a blog you just made. All you would need to do is type this command and press [Enter]:

wp theme install geocitites --activate

Let’s decompose that long command. First, just like with every other Linux program, the first word (“wp”) is telling the OS we want to run WP-CLI. Since the CLI works on a hierarchical basis, the type of command being executed comes right after the name of the program. Of course, this one is related to a theme, so it’s predictably just “theme”.

After giving the category of command to the program called “wp” we need to tell it what we want it to do. In this case, we need to install the theme onto the server. That’s why the next word in the command is just “install”. You would need to know the server-side theme name to run this command; this is set by the theme’s developers. The server-side file name is typically the same as the theme name.

You’re probably seeing the advantage of utilizing that hierarchical structure now. It greatly simplifies the amount of typing you need to do! After telling it which theme we want to install, we can add parameters. In this case, we could have just left the command as wp theme install geocities. This would have just installed the theme onto our server. However, it would not have actually applied the theme to anything. The “activate” parameter is what does this.

Speaking of parameters, you might be a bit shaky on them unless you work with Linux or Mac quite often. Let’s run through a quick refresher before we continue.

A Quick Primer On Linux Parameters, Arguments, and Operators

Before you go flying into the world of WP-CLI, you’ll need to understand how the Linux terminal interprets commands and three “operators”.

Unless you are already typing within a program, Linux requires you to punch in the “alias” of the program. This is the name of the “root package” in a repository, and in this case, it’s just “wp”. Keep in mind that, unlike with Windows, words in Linux are case-sensitive!

The text after the program name contains the “parameters” for the program. These parameters can either be mandatory or optional. In the case of our themes example, it’s clear what parameters were mandatory: the “theme” parameter, as this told us the general area where the program should look and the “install” parameter, as the program needs to know what actions to take.

Linux often uses shorthand to a fault, which is why most “arguments” have two ways of being expressed. Let’s say that a certain program had a “help” argument; in Linux, this is almost universally a “-h” in the command. There’s almost always the full-text version of the argument. This is preceded by two dashes, so it would read as: “–help”.

Last, there are a few operators you should know about. Just like in a math class, an operator shows that some action or computation is being performed. The three that are most common by far (and that you’re most likely to run into looking at others’ WordPress scripts) are the left bracket, the right bracket, and the pipe, which is the symbol ‘|’.

Because a left bracket appears as expanding to the right, this means that we are writing data from whatever is to the left of the symbol into whatever is to the right of the symbol. Likewise, when you see a right bracket, this means that the item to the right of the symbol is reading in data from what is to the left of it.

The pipe has been in common use for decades because of its simplicity and efficiency. All this does is run the program to the left of the pipe, take the data it outputs, and use that data as input for the program to the right of it. For example, if we had program1 | program2 we would be saying we want to run program1, take whatever it spits out, and drop it into program2 as it executes.

How Do I Access My WordPress Command Line?

Before you become an authority on WP-CLI, you’ll need to know how to get to it. The exact process will depend on the type of hosting you use.

Quick Aside

A few years ago, I gave a talk on using WP-CLI to run your own Managed WordPress hosting. I’m including the video from that talk and a link so a sample .sh file you could use to run and install WordPress.

Verify System Requirements

For both security reasons and performance purposes, you’ll need to make sure that your system meets the minimum dedicated standards. Though wp-cli.org will always have the most recent requirements, here are the requirements at the time of writing:

  • At least PHP 5.6+
  • At least WordPress 3.7+

Accessing WP-CLI On Dedicated Server Or VPS

When you rent a dedicated server or a VPS, you get administrative control. There is a certain volume of resources allocated to you, and you typically have full domain over how to use them.

If You’re On Windows

As in most cases, the Windows version of the program is far different from the Mac or Linux versions. Thankfully, you can reference the official project site for detailed instructions on putting it on a Windows box. As a general rule of thumb, you should try to avoid using Windows on anything “network-facing” because it’s notorious for having severe vulnerabilities that get uncovered after they’re exploited.

If You’re On Linux Or Mac

Because Mac is based on Unix, the parent of Linux, the process of getting the WordPress CLI installed on both is essentially identical. Here’s what you’ll need to do:

  1. First, download the software. Different versions of Unix and Linux utilize either wget or curl, so if one doesn’t work in the next step, try the other.
  2. The file we need to put into a private directory on our server is called wp-cli.phar. Thankfully, the creators always have the latest stable version at the same GitHub URL. Run this command once you’re in the directory where you’d like to download the file: curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar.
  3. It’s time to get the program working now. The chmod command changes permissions on a file. By default, Unix and Linux do not make downloaded files executable, but this command allows us to do that. In the same directory, run the command chmod +x wp-cli.phar.
  4. (Sorry, we know it’s a bit cumbersome) We will now need to move it into the main WordPress binary directory. This is where all of the little programs live that WordPress taps. The mv command can do this for us. Starting from the directory that contains the file, run this: mv wp-cli.phar /usr/local/bin/wp.
  5. At this point, everything should be good to go. There’s a built-in testing mechanism that allows us to verify this. In the terminal, just run the program and the full text argument of “info”. This would look like wp –info.
  6. Ensure that you get a screen with several lines about your new installation of WP-CLI. Then you’re good to continue!

Accessing WP-CLI On Shared Hosting

Unfortunately, there are almost an infinite number of variables to consider when it comes to shared hosting. Even the version of PHP you’re allowed to use is ultimately up to the server’s administrator. If it happens to be a Windows-based shared host, there will need to be some method already engineered to install this.

If it’s on a Linux or Mac server, it’ll come down to what type of access you have. Many shared hosts have a “virtual terminal” you can use. This is a restricted version of the actual OS-level terminal. It allows you to perform commands that the administrator deems to be free of threats to other users. Should this be the case, simply look at the section above and follow those steps in your virtual terminal.

It’s also possible that you have access to an installer. Most hosts that use something like cPanel attach installers for hundreds of different systems. The idea is that this will lead to less support tickets which will help them to operate more efficiently. If you see that the CLI is available to install via a GUI host-based installer, that’s your best option.

Finally, if it’s a Linux or Mac server, there’s no obvious option to install this software. Simply put a support ticket in with your host and ask for it to be added to the list of apps. Hosts usually are looking to cater to the widest audience as possible, so this would be a win-win.

WP-CLI Commands

We’ve talked a lot about how to get the program up and running, but we’ve discussed very little about how to use it effectively. If you have experience in programming, it’s a similar process. As you complete tutorials and learn more commands and shortcuts, you’ll automatically memorize the ones that are most critical for you personally.

Keep in mind that for use in the real world, you always need a decent WordPress security plugin installed before toying with your server. The last thing you’d want to happen is for your data to be compromised because you incorrectly entered a keystroke and have no way to fix it other than starting over!

While it’s possible to enter single commands at a time, this is a very inefficient method. It’s much better to create .sh files with a series of WordPress commands; this is how you can get what might take thirty minutes in the GUI done in seconds.

In the tutorial we’ll run through shortly, we’ll make a script from scratch that makes a brand new WordPress server, including a predefined configuration. Unfortunately, we could spend all year going over every last command and how it works. Because of its large user base, WP-CLI has quite good documentation.

Specifically, they have a handbook. It’s a lengthy document and will require patience. However, it goes over virtually every command, what it does, why it’s important, and more. The publication also covers how you can combine various commands to create your own scripts.

From there, the possibilities are endless.

Updating WP-CLI

Looking to grab the latest version of the WordPress CLI? That’s a great idea since you never know when a malevolent security researcher has found a vulnerability in older versions!

When you go to update the software, it checks your current version against the one published by WordPress. Unlike many Linux applications, the developers created a very simple one-liner you can enter into the terminal to ensure you’re running the latest version. Best of all, it doesn’t interfere with your current sites.

Keep in mind that this “easy update” method will only work if you installed it by grabbing that wp-cli.phar file we mentioned earlier. Some tutorials cite alternative installation methods, such as using Docker, etc. You’ll need to hop through more hoops than we can fit on this page to update if that’s how you installed it.

Here are the steps to an update in under a minute:

  1. Open up the terminal you used to initially install the software.
  2. Determine the risk level you’re willing to accept. The “nightly” version will have all the very latest features, but it may not be stable. We recommend grabbing the “stable” version.
  3. Enter the command wp cli update. This will grab the latest stable version by default.
  4. If you’re looking for the nightly build instead, just run the command wp cli update –nightly.

There are plenty of ways to make the update process your own. For example, if you don’t feel like going through confirmation dialogs, you don’t have to. You would just need to add the –yes parameter to force-answer “yes” to each dialog.

Whether you’re just getting started or not, taking a look at the full page of parameters and tinkering on a test server is a great idea.

WP-CLI Tutorial

Now that we’ve covered the prerequisites let’s take the WordPress CLI for a spin! One common task of WordPress admins is to create new sites. Going through the GUI wizard is an option, but the easiest way is to use WP-CLI.

Imagine you need to create ten different blogs with mostly the same settings. Sure, you could perform this manually if you would like to spend hours on the task. You could also create a reusable script and simply run it for each site! We’re going to make our own CLI script. We recommend that you type in each command rather than copy and paste. Make sure you put it in a .sh file so you can easily run it later.

  1. Open your favorite raw text editor. Notepad on Windows is not recommended for this purpose. Two great ones you can try out are GitHub’s Atom and open-sourced Notepad++. Both are multi-platform.
  2. Save the file as any name that has a “.sh” extension. For example, top-wordpress-dev-in-the-world.sh would work.
  3. Before we type anything, you should open up the full commands list. Feel free to deviate from our script to make your own custom one as you go along.
  4. The first few lines use the core category. First, we need wp core download to grab the latest version.
  5. Next, we need to input our database credentials. Of course, you’ll need to use more secure ones, but here’s an example: wp core config –dbname=customdbname –dbuser=admin –dbpass=password –dbhost=localhost –dbprefix=customprefix_ –extra-php ‹‹PHP
  6. In that line, we actually created a wp-config file with all that data. –extra-php ‹‹PHP is what finalizes that file.
  7. That ‹‹PHP also lets us drop a few PHP variables in. We will need these if we want to have logs to debug later. Add this line: define( ‘WP_DEBUG’, true );, followed by define( ‘WP_DEBUG_LOG’, true );. On the next line, just put PHP to show we’re done manually writing PHP code.
  8. We did make wp-config.php, but there’s one more line that has to be added to activate our DB: wp db create.
  9. The only line left is the one that finalizes the whole WordPress installation! wp core install –url=http://yoursite.com –title=MainWPTitle –admin_user=username –admin_password=password –admin_email=admin@yoursite.com. Of course, you’ll need to change some of those parameter values. Other than that, we just made a script to generate new WordPress sites!

Using WP-CLI With Plugins

Plugin management is likely not your favorite thing in the world, but it’s necessary. We can use the CLI to make plugin management far easier and faster.

By far, the most useful feature is bulk plugin installation. You first will need to gather the names of the plugins you’d like to install. These can be found in the official WordPress URLs. Let’s say you want the jetpack plugin. Because the official store URL for this would be https://wordpress.org/plugins/jetpack/, we take the last part of that URL (jetpack).

The command wp plugin install can take an unlimited number of plugin names. That means you can grab ten plugins in seconds rather than manually search for and install each one!

Spread the love

Posted by News Monkey