Articles on: Control Panel
This article is also available in:

How to install and manage WordPress from the command line with WP CLI?How to install and manage WordPress from the command line with WP CLI?

WordPress is incredibly easy to use with its intuitive GUI. However, it is not ideal for server administrators and hosting providers who manage hundreds of sites. WP-CLI (WordPress Command Line Interface) is a powerful tool designed specifically for this purpose. It can control all aspects of WordPress from the command line.

When you are tasked with updating or installing a plugin, managing multiple WordPress sites with the command line and scripts is more efficient. We will explain how to install WP-CLI and highlight some of its most useful features. We will see how to install, update and manage the WordPress kernel, plugins and themes on the command line.

Install WP-CLI



WP-CLI is a standalone PHP application. It can be installed by server administrators and ordinary users of cPanel.

Server administrators can make WP-CLI available to all server users.
cPanel users can install it in their home directory or in a WordPress site directory to control their sites.

To install and use WP-CLI, you must access the command line of your server. To do this, use your root access by connecting to the server via your SSH access.

From your SSH access, use the following command line to download WP-CLI to your server.

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

Then you still need to set permissions before you can use it. To do this, now enter the following command line:

chmod +x wp-cli.phar

Configure WP-CLI as cPanel user



cPanel users without root access cannot move files to a PATH directory. However, they can run it from their home folder or in the directory of a WordPress site.

To run it from your home directory, you can tell WP-CLI which WordPress site to control with the–path option. For example, the command should look like this (but may vary depending on the folder chosen):

. /wp-cli.phar config list --path=/home/user/public_html/

If you move WP-CLI to the WordPress site directory, you do not need to provide a location with “–path”. However, you will need to specify the directory that contains the executable, using «. / » for the current directory. In this case, the command should look like this:

/home/user/public_html/wp-cli.phar config list

To make it easier to use WP-CLI, you can create a domain alias. To do this, simply use the following command line shortcut:

alias wp='~/wp-cli.phar'

The shell will then automatically replace wp with ~/wp-cli.phar. This change will allow you to enter wp rather than the full path to the executable.

You can now make this alias permanent with the command:

echo "alias wp='~/wp-cli.phar'" >> . bashrc

This adds the alias command to your account’s .bashrc configuration file. It will then run every time you log in.

WP-CLI Command Operation



WP-CLI commands consist of a primary command followed by subcommands. These allow you to control particular aspects of a WordPress site.

For example, a simple command might look like this:

wp help

In this example, “help” is a master command. It has available subcommands, such as:

wp help core

This command/subcommand set prints help information for the "kernel" management functions. This tool has an excellent integrated support and documentation system. If you don’t know what commands are available or their function, help should be your first resort.

4 incredibly useful WP-CLI commands



There are more than 40 commands and hundreds of subcommands executable on WP-CLI. You can read the full list in the documentation. However, we present some of the most useful.

Read and write configuration files
Changing passwords for WordPress users
Install WordPress kernel, themes and plugins
Backup and optimize WordPress database

Read and write WordPress configuration



The config command can read and write the WordPress configuration, which is stored in the wp-config.php file.

To see the configuration variables in a site’s wp-config.php file, use the following command:

wp config list

If you want to change individual configuration variables such as the database name, use this command instead:

wp config set DB_NAME new_name

To generate a new wp-config.php file with preconfigured values, you must use the following command:

wp config create --dbname=user2_wp --dbuser=user2_wp --dbpass=new_password

Changing passwords for WordPress users



WP-CLI offers a faster way to replace lost and forgotten WordPress passwords. To do this, simply use the following command:

wp user update USERNAME --user_pass="new_password"

Although this method is faster, it is not the most secure. The user’s clear password is then stored in your shell history. You can delete shell history entries by using the up arrow to select the command and pressing Ctrl-U to delete it.

Installing WordPress Core, themes and plugins



One of the most useful aspects of controlling WordPress from the command line is the ability to install everything from a plugin to a full WordPress site.

Let’s start with a plugin:

wp plugin install hello-dolly --activate

To install without enabling, omit the “–enable” option. To find the correct plugin name, open its page in the WordPress plugin catalog and copy the slug URL. In the example, we used the Hello Dolly plugin and copied the slug URL from its webpage: https://wordpress.org/plugins/hello-dolly/.

To install a theme:

wp theme install twentytwenty --activate

You can also «uninstall», «delete» and «update» plugins and themes. The update feature is particularly useful on sites with many plugins:

To update all plugins of a site at the same time:

wp plugin update --all

Finally, to install a new WordPress site in seconds:

wp core install --url=example.com --title="A New Site" --admin_user=frank --admin_password=astrongpassword --admin_email=frank@example.com

Running on the command line stores the administrator password in plain text in the shell history, but you can remove it as described in the previous section.

Backup and optimize WordPress database



As we explained in How to Backup and Restore MySQL® Databases in cPanel, it is easy to empty your site’s MySQL database in the cPanel interface. However, if you prefer to back up from the terminal, use:

wp db export --add-drop-table

The “–add-drop-table” option ensures that the data is correctly replaced when restoring the backup. Export creates a SQL file with a file name based on the date and name of the database. To specify a different file name, add it at the end of the command:

wp db export --add-drop-table database-backup.sql

To restore the database, import the SQL file with:

wp db import database-backup.sql

The import is an action with a destructive side. It will permanently delete all data that has been added to the database after the time of backup.

Finally, you can also optimize or repair the database. First, optimization reorganizes how data is stored. This speeds up reading and writing. To do this, use the following command:

wp db optimize

Repair is an attempt to repair damaged database tables. This is sometimes worth trying if you suspect database corruption or if you are facing a white screen of death.

wp db repair'

Fast and efficient WordPress multisite management with WP-CLI



WP-CLI is an essential tool if you host and manage a large number of WordPress sites. It can significantly reduce the time and effort required to perform regular maintenance tasks.

In this article, we focused on manual order execution. All of these commands and more can be used in scripts. They automate complex workflows.

WP-CLI can also be combined with Cron to schedule WordPress management tasks. If using WP-CLI still seems complex, cPanel has added the most used WP-CLI commands to the WordPress Toolkit for cPanel in version 92. Please feel free to consult this reference document if necessary.

Updated on: 11/09/2023

Was this article helpful?

Share your feedback

Cancel

Thank you!