This tutorial covers the following topics:

 

WP-CLI is the command-line interface for WordPress, allowing you to manage all aspects of the application from the command prompt, without having to use a web browser. It's a very powerful tool that could help you complete complex tasks that would otherwise require additional plugins or would take much more time to accomplish.

This tutorial focuses on using WP-CLI to complete typical WordPress administration tasks, such as updating themes and plugins, managing the dashboard users, and changing the site's URL settings. Note that WP-CLI is available on all servers, but it requires SSH access. Thus, you will first need to make sure that you have enabled SSH access for your account. You can do that through the "SSH Access" section of the hosting Control Panel. More information on SSH is available in the following articles from our online documentation:

Once logged into your account over SSH, you will need to navigate to the location of your WordPress site. If you are looking to manage the site for your main domain name and you have not changed the default web root folder, you will need to run the following command:

cd ~/www/www/

If your WordPress site is located in another directory, you will need to navigate to that directory instead. Now that you are in the WordPress directory, you can start executing WP-CLI commands.

Obtaining a list of the available WP-CLI commands

You can obtain a list of all the available WP-CLI commands by running the following

wp help

Updating the WordPress core

You should first check the current version of your WordPress installation by running the following command:

wp core version

You can start the update with the following command:

wp core update

If there are no updates available, you will receive the following message:

Success: WordPress is up to date.

Updating WordPress plugins

Again, you should first check the status of the installed plugins, and that's done by running:

wp plugin list
+---------------------+--------+-----------+---------+
| name | status | update | version |
+---------------------+--------+-----------+---------+
| all-in-one-seo-pack | active | none | 2.9.1 |
| contact-form-7 | active | none | 5.0.5 |
| fusion-builder | active | available | 1.1.6 |
| fusion-core | active | available | 3.1.6 |
| jetpack | active | none | 6.7 |
| LayerSlider | active | available | 6.3.0 |
| revslider | active | none | 5.4.2 |
| the-events-calendar | active | available | 4.6.25 |
| wp-mail-returnpath | active | none | 1.0.3 |
+---------------------+--------+-----------+---------+

We have a total of nine plugins installed in our sample WordPress project. You can see under the Status column that all of them are currently active and there are updates available for four of them. Here is how we can update one of the plugins:

wp plugin update fusion-builder
Enabling Maintenance mode...
Downloading update from https://updates.theme-fusion.com/?avada_action=get_download&item_name=Fusion%20Builder&nonce=27c6b23b34&t=1542808142&ver=5.7.1...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Disabling Maintenance mode...
+----------------+-------------+-------------+---------+
| name | old_version | new_version | status |
+----------------+-------------+-------------+---------+
| fusion-builder | 1.1.6 | 1.7.1 | Updated |
+----------------+-------------+-------------+---------+

Installing and activating/deactivating plugins

 Here are the commands for installing, activating, and deactivating plugins:

wp plugin install plugin_name
wp plugin activate plugin_name
wp plugin deactivate plugin_name

You should replace the plugin_name string with the actual name of the plugin. Deactivating plugins via the command line can be quite helpful if you have a plugin that has caused an issue with your website and yet you cannot log in at the dashboard with a browser, in order to deactivate it. In such cases, you would still be able to log into your account via SSH and deactivate the problematic plugin from the command line using WP-CLI. 

Managing WordPress themes

Here are the most common commands for managing the WordPress themes:

wp theme list
wp theme activate theme_name
wp theme delete theme_name
wp theme install theme_name
wp theme update theme_name

The first command shows you a list of the themes currently installed, and here is the output when executed in our sample installation:

wp theme list
+-----------------+----------+-----------+---------+
| name | status | update | version |
+-----------------+----------+-----------+---------+
| twentyfifteen | inactive | available | 1.8 |
| twentyseventeen | inactive | available | 1.3 |
| twentysixteen | inactive | available | 1.3 |
+-----------------+----------+-----------+---------+

This table shows there are three themes installed with updates available for all of them. Here is how a theme can be updated via the command line:

wp theme update twentyseventeen
Downloading update from https://downloads.wordpress.org/theme/twentyseventeen.1.7.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the theme...
Theme updated successfully.
+-----------------+-------------+-------------+---------+
| name | old_version | new_version | status |
+-----------------+-------------+-------------+---------+
| twentyseventeen | 1.3 | 1.7 | Updated |
+-----------------+-------------+-------------+---------+
Success: Updated 1 of 1 themes.

Managing WordPress users

  • You can obtain a list of the existing users with the following command:
wp user list
  • The following command can be used to change the password of a given user:
wp user update USERNAME --user_pass="PASSWORD"

This command is helpful if you have forgotten the password for a given user and do not have access to the email account associated with it.

  • Here is how you can create a new dashboard user directly from the command line:
wp user create USERNAME EMAIL --role=administrator

You should replace the USERNAME string with the desired username and the EMAIL with his/her actual email address. The "role" field sets the role of the user to create; the possible values are: ‘administrator’, ‘editor’, ‘author’, ‘contributor’, ‘subscriber’. Here is an example:

wp user create example user@example.com --role=administrator

Changing the WordPress URL

CMS applications like WordPress are configured to work and accept requests over a specific URL. This URL is set during the installation process. However, it's often required to change it for a number of different reasons. For example, you might have to move your WordPress installation from one subdomain or subfolder to another, or may wish to change your domain name. Another fairly common situation is whenever you wish to force HTTPS connections to your website after installing an SSL certificate. This is a fairly complex task as it requires an update of all the database entries containing the old URL to the new one. The process is much easier with the use of WP-CLI, as you just need to run the following command:

wp search-replace 'http://your-old-URL.com' 'http://your-new-URL.com' --skip-columns=guid

Your site should now be configured to work over the new URL. Don't hesitate to open a support ticket in case you detect any issues.

Note: There may be hard-coded links to resources in your files that contain the old URL. If you notice missing images, CSS files, or any other discrepancies, you may want to check the browser's error console for specific errors. You can open the error console by clicking F12. It will help you locate files that may contain links to missing resources. You may also open a support ticket for help.