WP-CLI v2: Managing WordPress Sites from the Command Line

WP-CLI is the official command line tool for interacting with and managing your WordPress sites, you can add/remove users, posts, categories, search and replace in the database, reset passwords, troubleshoot performance issues, and much more without needing to log in to the dashboard and navigate …

WP-CLI is the official command line tool for interacting with and managing your WordPress sites, you can add/remove users, posts, categories, search and replace in the database, reset passwords, troubleshoot performance issues, and much more without needing to log in to the dashboard and navigate through the pages.

Using WP-CLI to manage the WordPress installation over the conventional GUI is much more efficient, you can also use WP-CLI in Bash scripts to automate tasks that are tedious or take a long time to perform.

WP-CLI can also come in really handy when you are locked out of your WordPress installation or have some sort of server or database error, the purpose of this tutorial is to serve as a WP-CLI commands cheat sheet of sorts.

Prerequisites

Although you can create a new WordPress installation using WP-CLI and I have covered that in the later section, I’ve assumed that you already have a WordPress installation to begin with.

Our guide on WordPress installation using EasyEngine can help you install and configure a WordPress website, once done, SSH into your server using ssh username@123.45.67.89.

EasyEngine already comes with the latest WP-CLI, but it doesn’t install WP-CLI on the server, instead, each WP/PHP site has it own PHP container and that container has WP-CLI installed in it.

You can access the WP-CLI for each of the installed EasyEngine site using: sudo ee shell example.com and manage your site using WP-CLI from there.

We’ll be installing WP-CLI on the server, you’ll need a sudo user and a WordPress installation.

Step 1: Installing WP-CLI

We’ll need curl to download the script, install it if it doesn’t come with your installation: sudo apt install curl

The WP-CLI tool is packaged in a “Phar” file, a packaging format for PHP applications that makes app deployment and distribution convenient, download it using curl or wget:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Once you have downloaded the file, verify that it is working:

php wp-cli.phar --info

You should see something like this:

# Output
OS:     Linux 5.4.0-107-generic #121-Ubuntu SMP Thu XXX XX XX:XX:XX UTC 2022 x86_64
Shell:  /bin/bash
PHP binary:     /usr/bin/php8.0
PHP version:    8.0.17
php.ini used:   /etc/php/8.0/cli/php.ini
MySQL binary:
MySQL version:
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /root
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.6.0

Next up, let’s make the file executable:

chmod +x wp-cli.phar

And, move it to your /usr/local/bin/ directory and rename it to wp so that we can access WP-CLI from any directory by entering the wp command at the start of a prompt:

sudo mv wp-cli.phar /usr/local/bin/wp

Now, let’s check the installed version of WP-CLI:

wp cli version
# Output
WP-CLI 2.6.0

WP-CLI Commands Structure

The WP-CLI commands are pretty similar to other command line tools, starting with wp and followed by a command, followed by any required and optional parameters:

wp command subcommand requiredparameters --optionalparameters --optionalparameters2=value

We’ll explore some commands like this in later sections.

Step 2: Installing WordPress

Installing WordPress using WP-CLI is a pretty straightforward four-step process, as explained in the official documentation:

Step 2.1: Downloading WordPress

wp core download

Step 2.2: Generating wp-config.php file

Now, we’ll need to generate a config file and set up the database credentials for our installation.

wp config create --dbname=your_db_name_here --dbuser=your_db_user_here --prompt=dbpass

This will generate the wp-config.php file and adds to it the database credentials that you passed.

Make sure to replace your_db_name_here with the name you want to assign to the database, replace your_db_user_here with your database user, and type the database password when prompted.

Step 2.3: Creating Database

wp db create

This will create the database based on the information we passed to the wp-config.php file in the last step.

Step 2.4: Installing WordPress

Now we are ready to install WordPress:

wp core install --url=example.com --title="your_site_title_here" --admin_user=your_username_here --admin_password=your_password_here --admin_email=admin@example.com

Be sure to update url, title, admin_user, admin_password, and admin_email.

Using WP-CLI

Now, we are ready to manage our WordPress installation using WP-CLI, first make sure you are in the correct WordPress installation directory:

cd /var/www/mywordpressdirectory

Replace “mywordpressdirectory” with the directory that contains your WordPress installation, it’ll be your domain name, if you followed the prerequisite tutorials.

I haven’t tested the above method, and use EasyEngine, if you have installed WordPress using our EasyEngine tutorial, switch websites using ee shell example.com.

Step 3: Configuring WordPress Plugins

This section will cover installing, updating, and deleting plugins on a WordPress site through the command line.

Step 3.1: Viewing Installed Plugins

You can list the currently installed plugins using the following command:

wp plugin list

You should see something like this with a list of plugin names along with their status, version, and an indication of an available update:

+------------------+----------+-----------+---------+
| name             | status   | update    | version |
+------------------+----------+-----------+---------+
| akismet          | inactive | available | 4.2.2   |
| hello            | inactive | none      | 1.7.2   |
| nginx-helper     | active   | none      | 2.2.2   |
| wp-redis         | active   | none      | 1.2.0   |
| object-cache.php | dropin   | none      |         |
+------------------+----------+-----------+---------+

Step 3.2: Searching for Plugins

wp plugin search seo
Success: Showing 10 of 1010 plugins.
+------------------------------------------------------+---------------------+--------+
| name                                                 | slug                | rating |
+------------------------------------------------------+---------------------+--------+
| Yoast SEO                                            | wordpress-seo       | 96     |
| All in One SEO – Best WordPress SEO Plugin – Easily  | all-in-one-seo-pack | 92     |
| Improve SEO Rankings & Increase Traffic          |                     |        |
| Rank Math SEO – Best SEO Plugin For WordPress To Inc | seo-by-rank-math    | 98     |
| rease Your SEO Traffic                               |                     |        |
| The SEO Framework – Fast, Automated, Effortless.     | autodescription     | 98     |
| SEOPress, on-site SEO                                | wp-seopress         | 98     |
| W3 Total Cache                                       | w3-total-cache      | 88     |
| SEO Plugin by Squirrly SEO                           | squirrly-seo        | 92     |
| Slim SEO – Fast & Automated WordPress SEO  | slim-seo            | 92     |
| Plugin                                               |                     |        |
| Redirection                                          | redirection         | 88     |
| LiteSpeed Cache                                      | litespeed-cache     | 96     |
+------------------------------------------------------+---------------------+--------+

You can go to the next page by using the --page flag:

wp plugin search seo --page=2

Note down the value in the slug column to install a particular plugin.

Step 3.3: Installing Plugins

You can install one or more plugins by using the wp plugin install command followed by the slug as shown in the last section.

You can also find the slug from the URL page of a particular plugin, it’s the last word in the URL, for example, the slug of a plugin “The SEO Framework” whose plugin URL is: https://wordpress.org/plugins/autodescription/ is “autodescription”.

wp plugin install autodescription

You can also install multiple plugins at once by putting the slugs one after the another: wp plugin install jetpack gutenberg

The output will show progress and completion of the installation of each of the plugins:

# Output

Installing The SEO Framework – Fast, Automated, Effortless. (4.2.4)
Downloading installation package from https://downloads.wordpress.org/plugin/autodescription.4.2.4.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.

You can also install a plugin from a remote source other than the WordPress plugin repository by passing the zip file’s URL as an argument: wp plugin install https://example.com/wp-content/uploads/myplugin.zip

Step 3.4: Activating and Deactivating Plugins

You can install and activate plugins in one go by appending the --activate flag to wp plugin install:

wp plugin install autodescription --activate

To activate or deactivate one or more plugins, use the activate and deactivate commands:

# Activate plugins
wp plugin activate jetpack gutenberg

# Deactivate plugins
wp plugin deactivate jetpack gutenberg

You can also activate or deactivate all plugins at once using --all flag:

# Activate all plugins
wp plugin activate --all

# Deactivate all plugins
wp plugin deactivate --all

Step 3.5: Updating Plugins

You can update plugins using:

wp plugin update autodescription

Update all plugins by appending --all flag to the above command:

wp plugin update --all

Step 3.6: Deleting Plugins

You can delete plugins using:

wp plugin delete redirection

Delete all plugins by appending --all flag to the above command:

wp plugin delete --all

Step 4: Configuring WordPress Themes

WP-CLI commands for managing WordPress themes are pretty similar to the ones used for managing plugins.

Step 4.1: Viewing Installed Themes

wp theme list
#Output

+-----------------+----------+--------+---------+
| name            | status   | update | version |
+-----------------+----------+--------+---------+
| twentytwenty    | inactive | none   | 1.9     |
| twentytwentyone | inactive | none   | 1.5     |
| twentytwentytwo | active   | none   | 1.1     |
+-----------------+----------+--------+---------+

Step 4.2: Searching for Themes

wp theme search generatepress
# Output

+---------------+---------------+--------+
| name          | slug          | rating |
+---------------+---------------+--------+
| GeneratePress | generatepress | 100    |
+---------------+---------------+--------+

Step 4.3: Installing and Activating Themes

Just like plugins, use the slug to install and activate themes:

wp theme install generatepress --activate
# Output

Installing GeneratePress (3.1.3)
Downloading installation package from https://downloads.wordpress.org/theme/generatepress.3.1.3.zip...
Unpacking the package...
Installing the theme...
Theme installed successfully.
Activating 'generatepress'...
Success: Switched to 'GeneratePress' theme.
Success: Installed 1 of 1 themes.

Step 4.4: Updating Themes

Update one or all plugins using:

wp theme udpate --all

Step 4.5: Deleting Themes

Delete one or all themes using:

wp theme delete twentytwenty
# Output

Deleted 'twentytwenty' theme.
Success: Deleted 1 of 1 themes.

Step 5: Managing Database

WP-CLI can come really handy when it comes to managing the MySQL database, open up an interactive MySQL database session using:

wp db cli
# Output

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.5.4-MariaDB-1:10.5.4+maria~focal-log mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [example_com]>

You can then use the MySQL shell as you normally would and, once you are through with your tasks, exit the shell by typing exit.

I’ll be creating a new post with nifty MySQL commands soon, until then, this guide on W3Schools serves well.

Step 5.1: Backing Up and Restoring

You can also take back up of your entire WordPress database using WP-CLI using:

wp db export

This will create a SQL dump file in the current directory which will contain your entire WordPress database including your posts, pages, user accounts, menus, and so on.

You can also restore to an existing SQL file by importing it into your database, you’ll need to upload the MySQL file first onto your server using sftp, and then use this command:

wp db import file.sql

This command can come really handy when migrating a WordPress website from one location to another.

Step 5.2: Searching and Replacing

After you have migrated the site using the earlier wp dp import command, you’ll have to serialize the arrays of your database, this is where the search-replace command comes to save the day.

It un-serializes the arrays first, then performs the search and replace, then re-serializes the arrays using one command:

wp search-replace oldstring newstring

So, for example, if you have migrated your site from example.com to example.net, you’ll use this command:

wp search-replace example.com example.net

You can also do things like optimizing the database, viewing database tables, deleting a database, or resetting one, explore such options wp help db command.

Step 6: Managing Users

WP-CLI has pretty nifty commands for creating new users, updating existing users, deleting users, and more.

Step 6.1: View all Users

wp user list

Step 6.2: Create a New User

wp user create username username@example.com --role=author

You can assign other roles using flags: ‘administrator’, ‘editor’, ‘author’, ‘contributor’, ‘subscriber’; the password for the user can also be assigned using the --user_pass flag.

Step 6.3: Update an Existing User

To update an existing user, you’ll need to use the ID of that user, which you can get from the wp user list command, here’s an example updating user with ID 123:

wp user update 123 --display_name=Mary --user_pass=marysecretpassword

You can update other things using specific flags like: --display_name, --role, --user_email, --first_name, etc.

Step 6.4: Deleting Users

You can delete a user and assign all their posts to a different user, here’s an example deleting the user 123 and assigning their post to user 456:

wp user delete 123 --reassign=456

Step 7: Maintenance Mode

WP-CLI can help enable, disable maintenance mode and check if maintenance mode is enabled or not.

Step 7.1: Check Maintenance Mode Status

wp maintenance-mode status

Step 7.2: Enable Maintenance Mode

wp maintenance-mode activate

Step 7.2: Disable Maintenance Mode

wp maintenance-mode deactivate

Step 8: Updating WordPress

You can update the core WordPress file with WP-CLI, check the current version of WordPress that you have installed using:

wp core version

Step 8.1: Check for Updates

wp core check-update

Step 8.2: Update WordPress

wp core update

More Useful Commands

To start using WP-CLI, type wp and hit Enter, it will list all commands for managing the WordPress application through WP-CLI.

To get more details about a particular command and its options, type:

wp help <command name>

For example, to get more details about plugin command: wp help plugin

# Viewing WP-Cron events
wp cron event list

# Delete all Transients
wp transient delete --all

That’s all folks!

If you haven’t already tried WP-CLI, I urge you to give it a go. There are also a bunch of community commands like WP-CLI-SSH, which add even more great features! If you’re running into problems, make sure to check out the WP-CLI common issues documentation.

Leave a Comment