WP-CLI v2 – Managing WordPress From the Terminal
- Category : Server Administration
- Posted on : Apr 25, 2018
- Views : 3,151
- By : Zane P.

For most of its life, WordPress has been built using a simple code-base with a dash of object-oriented PHP being the most abstract system. In the past few years, however, this is changing for the better. From unit testing to CSS preprocessing and command line tools, more and more developer-friendly assets are popping up. In this article, we’ll look at one of my favorites: WP-CLI.
What is WP-CLI?
WP-CLI is a command line tool for developers to manage common tasks (and not so common) of a WordPress installation. It can add/remove users, posts, categories, insert test data, search and replace in the database, help troubleshoot performance issues, and much more!
WP-CLI has been an open source project for over a decade, being maintained primarily by Daniel Bachhuber since 2003. The primary goal of WP-CLI is to help speed up WordPress developer’s workflows.
Over the years the project has emerged into so much more! It’s now even becoming a requirement for other open source projects such as Trellis and Bedrock. As of January 2017, WP-CLI officially moved to WordPress.org and is also now being maintained co-maintained by Alain Schlesser.
WP-CLI v2 was released on August 8th, 2018, so we’ll also explore some of the changes and new features. If you’re a Host SEO client, WP-CLI v2.0.1 is installed by default on all of our servers, simply SSH into your server to get started. SSH access is included in all of our hosting plans.
- Getting WP-CLI
- The Basics Of WP-CLI
- WP-CLI Commands in General
- Useful Examples
- Using WP-CLI Remotely
- Using Bash Scripts
Getting WP-CLI
The minimum PHP requirement on WP-CLI v2.0.0 has been bumped up to PHP 5.4. While this is a good move ahead, we recommend that you at least run a supported version of PHP, meaning 5.6 or higher. PHP 7.2 is default on all Host SEO installs, both for security and performance reasons. We also have PHP 7.3 available.
To get started you’ll need to install WP-CLI – a very simple process. The steps for Linux and OSX are the following, issue these three commands one after the other:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
If something goes wrong or you are on Windows refer to the basic instructions or the alternative installation methods.
Once done you should be able to issue the wp --info command and get a meaningful response.
The process is the same if you want to install WP-CLI on your server. Remember, for Host SEO clients WP-CLI is already installed. Not sure which version you’re currently running? You can always issue the wp cli version command to find out.
The Basics Of WP-CLI
Having access to WordPress from the command line is powerful in and of itself but can give you even more control and speed gains when using bash scripts.
Bash scripts allow you to run a sequence of commands with a single command. You could type bash install-and-setup.sh and get the following result:
- Download WordPress
- Create and populate
wp-config.php - Create the database
- Install WordPress
- Install and activate any plugins you need
- Install and activate a theme
- Download and add test content
These would be the steps I would take to create a new test environment for a project. Normally it would take me 5-10 minutes at least, especially if there are a few plugins involved. Issuing a single command is obviously a lot faster.
WP-CLI Commands in General
If you are used to working in the terminal there’s nothing special about WP-CLI for you. Commands always start with wp followed by a command and subcommand, followed by required and optional parameters, something like this:
wp command subcommand requiredparam --optionalparam --optionalparam2=valueLet’s install a theme to see how this works with a real command:
wp theme install twentyseventeen --activateThis will install and activate the Twenty Seventeen theme on your WordPress installation.
Note that WP-CLI will work with the WordPress installation you are currently in the terminal. If you switch directories to go to another WordPress installation, it will work with that one.
Useful Examples
That was WP-CLI in a nutshell! While there are a few advanced things you can do, which we’ll get to in a moment, you already know enough to get started and do whatever you need to. I recommend taking a look at the commands list, try some of them out. We’ll take a look at some useful stuff here, then move on to using WP-CLI over SSH and using bash scripts.
Installing WordPress
I use WP-CLI a lot to set up test environments, the first step of which is a vanilla installation. Here is a list of commands I run:
wp core download
wp core config --dbname=mydbname --dbuser=mydbuser --dbpass=mydbpass --dbhost=localhost --dbprefix=whebfubwef_ --extra-php <<PHP
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
PHP
wp db create
wp core install --url=http://siteurl.com --title=SiteTitle --admin_user=username --admin_password=mypassword --admin_email=my@email.com
Note how cool this is! The most recent version of WordPress is downloaded using the first command. The second command sets up the config file with the database access and some additional PHP at the end. The additional constants make sure we have our debugging options on for testing.
The third command creates the database (WP-CLI uses the database access info from the config file) and finally, we install WordPress using a couple of parameters.
Reinstall WordPress Core
You can also reinstall WordPress core using WP-CLI. The following command would download WordPress core without the default themes and plugins.
wp core download --skip-content --force
Change WordPress URL
There are many reasons why you might need or want to change your WordPress URL. Perhaps you are changing domains, moving to a subdomain, updating from www to non-www, moving files around, or even migrating from HTTP to HTTPS. Whatever the case may be, you can use easily use the wp option update command for this. Here is an example below:
wp option update home 'http://example.com' wp option update siteurl 'http://example.com'
List of Current Plugins with Details
To get a list of current plugins installed on a site simply use the following command. In this example, you can see we have the Schema and Yoast SEO plugin installed. It will also return the status (active/deactivated), if there is an update available, and the current version.
wp plugin list
Installing Multiple Plugins
To install multiple plugins you can simply pile on parameters. Here’s an example that downloads and activates 3 plugins:
wp plugin install advanced-custom-fields jetpack ninja-forms --activateNote that the plugin names come from their name in the repository. The easiest way to figure this out is to visit their page and look at the URL or to use wp plugin search searchterm which will give you a list in the terminal.

You can also install older versions of WordPress plugins if needed with the --version attribute.
wp plugin install wordpress-seo --version=4.8 --activate
Even cooler, you can install plugins from remote files, not just the repository which is handy if you’re developing a plugi
