WP-CLI - Managing WordPress from the Linux Terminal

If you are a system administrator and responsible for managing hundreds or thousands of WordPress websites then it is a very time-consuming process. You will need to log in to each WordPress control panel, install or update plugins and themes. This is the place where the WP-CLI comes into the picture. The

WP-CLI is a powerful command-line tool specifically designed to manage WordPress from the command line. You can manage multiple WordPress sites without log into the WordPress admin panel. With WP-CLI, you can perform several operations including, installing and updating plugins, themes, creating content, working with databases, and more.

In this post, I will show how to install and use WP-CLI to manage WordPress sites.

Prerequisites

  • A server running Ubuntu 20.04 with WordPress installed.
  • A root password is configured on the server.

Getting Started

First, you will need to update the APT package cache to your system. You can update it with the following command:

apt-get update -y

Once your system is updated, you can proceed to the next step.

Install WP-CLI

First, download the WP-CLI binary with the following command:

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

Once downloaded, set proper permission to the downloaded file:

chmod +x wp-cli.phar

Next, copy the downloaded binary to the system path with the following command:

cp wp-cli.phar /usr/bin/wp

Now, verify the WP-CLI version with the following command:

wp cli version --allow-root

You should see the following output:

WP-CLI 2.5.0

Manage Plugins with WP-CLI

In this section, we will learn how to search, install, update, and delete plugins on a WordPress site from the command line.

First, change the directory to your WordPress website with the following command:

cd /var/www/html/wordpress

To list all installed plugins on your WordPress site, run the following command:

wp plugin list --allow-root

You should see the following output:

+---------+----------+--------+---------+
| name    | status   | update | version |
+---------+----------+--------+---------+
| akismet | inactive | none   | 4.1.9   |
| hello   | inactive | none   | 1.7.2   |
+---------+----------+--------+---------+

To search for a specific plugin, run the following command:

wp plugin search cache --allow-root

You should see all caching related plugins in the following output:

Success: Showing 10 of 3688 plugins.
+--------------------------------------------------------------------------------+--------------------------+--------+
| name                                                                           | slug                     | rating |
+--------------------------------------------------------------------------------+--------------------------+--------+
| LiteSpeed Cache                                                                | litespeed-cache          | 98     |
| W3 Total Cache                                                                 | w3-total-cache           | 88     |
| WP-Optimize – Cache, Clean, Compress.                                    | wp-optimize              | 96     |
| WP Fastest Cache                                                               | wp-fastest-cache         | 98     |
| WP Cloudflare Super Page Cache                                                 | wp-cloudflare-page-cache | 98     |
| Redis Object Cache                                                             | redis-cache              | 92     |
| WP Super Cache                                                                 | wp-super-cache           | 86     |
| Autoptimize                                                                    | autoptimize              | 94     |
| Hummingbird – Optimize Speed, Enable Cache, Minify CSS & Defer Critical JS | hummingbird-performance  | 96     |
| Cache Enabler                                                                  | cache-enabler            | 88     |
+--------------------------------------------------------------------------------+--------------------------+--------+

Now, install the specific plugin from the above list with the following command:

wp plugin install wp-super-cache --allow-root

You should see the following output:

Installing WP Super Cache (1.7.3)
Downloading installation package from https://downloads.wordpress.org/plugin/wp-super-cache.1.7.3.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.

Now, confirm the plugin installed or not with the following command:

wp plugin list --allow-root

You should see the following output:

+----------------+----------+--------+---------+
| name           | status   | update | version |
+----------------+----------+--------+---------+
| akismet        | inactive | none   | 4.1.9   |
| hello          | inactive | none   | 1.7.2   |
| wp-super-cache | inactive | none   | 1.7.3   |
+----------------+----------+--------+---------+

To install the plugin from the specific source with the following command:

wp plugin install https://downloads.wordpress.org/plugin/caldera-forms.1.9.4.zip --allow-root

You should see the following output:

Downloading installation package from https://downloads.wordpress.org/plugin/caldera-forms.1.9.4.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.

To activate the installed plugin, run the following command:

wp plugin activate wp-super-cache --allow-root

You should see the following output:

Plugin 'wp-super-cache' activated.
Success: Activated 1 of 1 plugins.

To deactivate the installed plugin, run the following command:

wp plugin deactivate wp-super-cache --allow-root

You should see the following output:

Plugin 'wp-super-cache' deactivated.
Success: Deactivated 1 of 1 plugins.

To activate all plugins, run the following command:

wp plugin activate --all --allow-root

To update a specific plugin, run the following command:

wp plugin update akismet --allow-root

To delete a specific plugin, run the following command:

wp plugin delete wp-super-cache --allow-root

To delete all plugins, run the following command:

wp plugin delete --all --allow-root

Manage Themes with WP-CLI

In this section, we will show you how to install, update, search and manage themes with WP-CLI.

To list all themes installed in your WordPress site, run the following command:

wp theme list --allow-root

You should see the following output:

+-----------------+----------+--------+---------+
| name            | status   | update | version |
+-----------------+----------+--------+---------+
| twentynineteen  | inactive | none   | 2.0     |
| twentytwenty    | inactive | none   | 1.7     |
| twentytwentyone | active   | none   | 1.3     |
+-----------------+----------+--------+---------+

To search for a specific theme, run the following command:

wp theme search metro --allow-root

You should see all themes that match the word metro:

Success: Showing 4 of 4 themes.
+----------------+----------------+--------+
| name           | slug           | rating |
+----------------+----------------+--------+
| Metrolo        | metrolo        | 100    |
| MetroStore     | metrostore     | 100    |
| Metro Magazine | metro-magazine | 98     |
| Rara Magazine  | rara-magazine  | 0      |
+----------------+----------------+--------+

To install and activate the metro theme, run the following command:

wp theme install metro-magazine --activate --allow-root

You should see the following output:

Installing Metro Magazine (1.3.5)
Downloading installation package from https://downloads.wordpress.org/theme/metro-magazine.1.3.5.zip...
Unpacking the package...
Installing the theme...
Theme installed successfully.
Activating 'metro-magazine'...
Success: Switched to 'Metro Magazine' theme.
Success: Installed 1 of 1 themes.

To update all themes, run the following command:

wp theme update --all --allow-root

To delete a specific theme, run the following command:

wp theme delete metro-magazine --allow-root

Create and Manage Posts and Pages with WP-CLI

In this section, we will show you how to list, create and manage posts and pages with WP-CLI.

To list all posts of your WordPress site, run the following command:

wp post list --allow-root

You should get the following output:

+----+--------------+-------------+---------------------+-------------+
| ID | post_title   | post_name   | post_date           | post_status |
+----+--------------+-------------+---------------------+-------------+
| 1  | Hello world! | hello-world | 2021-06-09 14:51:29 | publish     |
+----+--------------+-------------+---------------------+-------------+

To delete a specific number post, run the following command:

wp post delete 1 --allow-root

To create a new post, run the following command:

wp post create --post_status=publish --post_title="How to Manage WordPress with WP-CLI" --edit --allow-root

To create a page instead of a post, run the following command:

wp post create --post_title="My new page" --post_status=draft --post_type=page --allow-root

To generate 30 posts with dummy data, run the following command:

wp post generate --count=30 --allow-root

To list all generated posts, run the following command:

wp post list --allow-root

You should see the following output:

+----+-------------------------------------+-------------------------------------+---------------------+-------------+
| ID | post_title                          | post_name                           | post_date           | post_status |
+----+-------------------------------------+-------------------------------------+---------------------+-------------+
| 7  | Post 2                              | post-2                              | 2021-06-09 15:00:57 | publish     |
| 8  | Post 3                              | post-3                              | 2021-06-09 15:00:57 | publish     |
| 9  | Post 4                              | post-4                              | 2021-06-09 15:00:57 | publish     |
| 10 | Post 5                              | post-5                              | 2021-06-09 15:00:57 | publish     |
| 11 | Post 6                              | post-6                              | 2021-06-09 15:00:57 | publish     |
| 12 | Post 7                              | post-7                              | 2021-06-09 15:00:57 | publish     |
| 13 | Post 8                              | post-8                              | 2021-06-09 15:00:57 | publish     |
| 14 | Post 9                              | post-9                              | 2021-06-09 15:00:57 | publish     |
| 15 | Post 10                             | post-10                             | 2021-06-09 15:00:57 | publish     |
| 16 | Post 11                             | post-11                             | 2021-06-09 15:00:57 | publish     |
| 17 | Post 12                             | post-12                             | 2021-06-09 15:00:57 | publish     |
| 18 | Post 13                             | post-13                             | 2021-06-09 15:00:57 | publish     |
| 19 | Post 14                             | post-14                             | 2021-06-09 15:00:57 | publish     |
| 20 | Post 15                             | post-15                             | 2021-06-09 15:00:57 | publish     |
| 21 | Post 16                             | post-16                             | 2021-06-09 15:00:57 | publish     |
| 22 | Post 17                             | post-17                             | 2021-06-09 15:00:57 | publish     |
| 23 | Post 18                             | post-18                             | 2021-06-09 15:00:57 | publish     |
| 24 | Post 19                             | post-19                             | 2021-06-09 15:00:57 | publish     |
| 25 | Post 20                             | post-20                             | 2021-06-09 15:00:57 | publish     |
| 26 | Post 21                             | post-21                             | 2021-06-09 15:00:57 | publish     |
| 27 | Post 22                             | post-22                             | 2021-06-09 15:00:57 | publish     |
| 28 | Post 23                             | post-23                             | 2021-06-09 15:00:57 | publish     |
| 29 | Post 24                             | post-24                             | 2021-06-09 15:00:57 | publish     |
| 30 | Post 25                             | post-25                             | 2021-06-09 15:00:57 | publish     |
| 31 | Post 26                             | post-26                             | 2021-06-09 15:00:57 | publish     |
| 32 | Post 27                             | post-27                             | 2021-06-09 15:00:57 | publish     |
| 33 | Post 28                             | post-28                             | 2021-06-09 15:00:57 | publish     |
| 34 | Post 29                             | post-29                             | 2021-06-09 15:00:57 | publish     |
| 35 | Post 30                             | post-30                             | 2021-06-09 15:00:57 | publish     |
| 36 | Post 31                             | post-31                             | 2021-06-09 15:00:57 | publish     |
| 5  | How to Manage WordPress with WP-CLI | how-to-manage-wordpress-with-wp-cli | 2021-06-09 15:00:39 | publish     |
+----+-------------------------------------+-------------------------------------+---------------------+-------------+

To generate a page with dummy data, run the following command:

wp post generate --count=30 --post_type=page --allow-root

Manage Database with WP-CLI

You can also manage the database with WP-CLI.

To backup your entire WordPress database, run the following command:

wp db export --allow-root

You should see the following output:

Success: Exported to 'mysite-2021-06-09-14d4641.sql'.

You can also import the WordPress database with the following command:

wp db import backup.sql --allow-root

Update WordPress with WP-CLI

To print the current version of your WordPress, run the following command:

wp core version --allow-root

You should see the following output:

5.7.2

To check for WordPress update, run the following command:

wp core check-update --allow-root

You should see the following output:

Success: WordPress is at the latest version.

You can now update the WordPress to the latest available version with the following command:

wp core update --allow-root

Conclusion

In the above guide, you learned how to install and use WP-CLI to manage the WordPress site. I hope this will make your job much easier.

Share this page:

1 Comment(s)