HostArmada Web Hosting Knowledge Base

Knowledge is power! Use that power and achieve total and unconditional control over the Web Hosting Services!

What is WP-Cli and how can I use it?

Over the past decade or so, WordPress has established itself to be the best platform website owners, and newcomers use to create their online projects. Starting as a simple blogging software, it has evolved into a web application not only available across all operating systems but having the ability to transform into whatever your vision of a website is. The possibilities it offers and the roles it can take on are literally limitless, and this tendency looks to be growing as the future unfolds.

What is WP-CLI?

An innovative feature introduced back in 2011 is the WP-CLI (WordPress Command-line Interface). Its purpose is to provide developers with an alternative method of managing their WordPress website. People who take advantage of its vast features are going to reduce the amount of repetitive tasks they perform dramatically. While the admin dashboard of WordPress is intuitive and easy to work with, a lot of website owners prefer using the terminal, and with the addition of WP-CLI, they never have to leave it!

Any task that you can do from the browser is made entirely possible to manage from the command-line interface utilizing the features WP-CLI brings to the table. If you master this incredibly flexible tool, you can save a lot of time and optimize your work when handling WordPress.

Being so vast and providing clients with this amount of utility, we at HostArmada have preinstalled WP-CLI on ALL OUR HOSTING PLANS. Clients can start using it immediately after they sign up!

What can WP-CLI help me achieve?

As we mentioned earlier, WP-CLI allows you to perform anything that you can do from the admin dashboard of your WordPress website. Here are some examples:

  • Update your WordPress version, themes, and plugins.
  • List all their plugins and their current status.
  • Bulk upload media files Create user accounts or edit current user accounts permissions

You are also able to perform tasks that are typically handled slowly with a single line, such as:

  • WordPress is domain sensitive, which means the domain name is hard-coded within the database or the wp-config.php file. With WP-CLI, you can run a search and replace command, allowing you to change this and configure WordPress to open with a different domain name.
  • You can create new WordPress installations.
  • You can create database backups or perform restorations of databases from previous dates.

The above is just a fraction of the opportunities that WP-CLI provides. In this article, we are going to introduce you to the most commonly used options and a few useful flags you can apply to them to enhance their output. Let's begin!

WP-CLI options and some useful examples

We cannot simply touch upon all the features of the WP-CLI tool. We will try to cover the most useful ones and give you examples that can get you started!

As the name suggests, WP-CLI is a command-line utility and as such it requires the user to be logged in the SSH service for the Web Hosting Account and of course to enter the directory where WordPress is installed. If you are not sure how to do that, we have great tutorials on how to Log in using SSHLinux Command Line Basics, and How to find the directory of your domain name.

Show all available options

To expand all the options WP-CLI has, please type in the following command:

wp help 

You will be provided with a big list of all the flags you can attach to WP-CLI

Update WordPress

First, you may want to check the current version of your WordPress installation. To do so, execute the following:

[user@server ~] wp core version 
5.2.2

You will get a single-line output containing the version of your application.

To update WordPress, all you need to do is type in the command:

[user@server ~] wp core update
Updating to version 5.4 (en_US)...
Downloading update from https://downloads.wordpress.org/release/wordpress-5.4-partial-0.zip...
Unpacking the update...
Success: WordPress updated successfully.

Tasks you can perform with plugins

There are multiple tasks you can control with the WP-CLI command when it comes to plugins, such as:

  • Install plugins
  • List plugins
  • Activate plugins
  • Deactivate plugins
  • Delete plugins

Here are a few examples:

Install

To install a plugin, please use the command:

wp plugin install "plugin-name" 

Here is an example:

[user@server ~]$ wp plugin install hello-dolly
Installing Hello Dolly (1.7.2)
Downloading installation package from https://downloads.wordpress.org/plugin/hello-dolly.1.7.2.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.

To immediately activate the plugin upon installation, please attach the --activate flag:

[user@server ~]$ wp plugin install hello-dolly --activate
Installing Hello Dolly (1.7.2)
Downloading installation package from https://downloads.wordpress.org/plugin/hello-dolly.1.7.2.zip...
Using cached file '/home/user/.wp-cli/cache/plugin/hello-dolly-1.7.2.zip'...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Activating 'hello-dolly'...
Plugin 'hello-dolly' activated.
Success: Installed 1 of 1 plugins.

The "install" flag is so powerful it can also accept URLs or archives as parameters. This is useful if you have purchased a plugin and have uploaded the archived files on your account, or you simply do not remember the exact plugin's name. You can look it up, find its URL in the WordPress plugin repository and then provide this parameter to the command.

Here are some examples:

[user@server ~]$ wp plugin install https://downloads.wordpress.org/plugin/hello-dolly.1.7.2.zip
Downloading installation package from https://downloads.wordpress.org/plugin/hello-dolly.1.7.2.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.
[user@server ~]$ wp plugin install hello-dolly.1.7.2.zip
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.

List

To list all plugins, please use this command:

[user@server ~]$ wp plugin list
+-------------+----------+--------+---------+
| name | status | update | version |
+-------------+----------+--------+---------+
| akismet | inactive | none | 4.1.4 |
| hello-dolly | active | none | 1.7.2 |
+-------------+----------+--------+---------+

To list only the active plugins, add the --status=active flag:

[user@server ~]$ wp plugin list --status=active
+-------------+--------+--------+---------+
| name | status | update | version |
+-------------+--------+--------+---------+
| hello-dolly | active | none | 1.7.2 |
+-------------+----------+--------+---------+

You may change the "active" with "inactive" to display only the inactive plugins.

Deactivate

To deactivate a plugin, please use the command:

[user@server ~]$ wp plugin deactivate hello-dolly
Plugin 'hello-dolly' deactivated.
Success: Deactivated 1 of 1 plugins.

Delete

To delete a plugin, use the command:

wp plugin delete "plugin name"

Example:

[user@server ~]$ wp plugin delete hello-dolly
Deleted 'hello-dolly' plugin.
Success: Deleted 1 of 1 plugins.

Bonus

Lastly, we are going to introduce you to the --all flag that can be combined with the "activate" or "deactivate" flags to form a powerful command that can disable or enable all plugins on your site:

[user@server ~]$ wp plugin activate --all
Plugin 'akismet' activated.
Plugin 'hello-dolly' activated.
Success: Activated 2 of 2 plugins.
[user@server ~]$ wp plugin deactivate --all
Plugin 'akismet' deactivated.
Plugin 'hello-dolly' deactivated.
Success: Deactivated 2 of 2 plugins.

Tasks you can perform with Themes

There are multiple tasks you can control with the WP-CLI command when it comes to themes, such as:

  • Install themes
  • List themes
  • Activate thems
  • Deactivate themes
  • Delete themes

Here are a few examples:

Install To install a theme, please use the command:

wp theme install "theme-name" 

Here is an example:

[user@server ~]$$ test.receptitenamama.net]$ wp theme install twentytwenty
Installing Twenty Twenty (1.2)
Downloading installation package from https://downloads.wordpress.org/theme/twentytwenty.1.2.zip...
Unpacking the package...
Installing the theme...
Theme installed successfully.
Success: Installed 1 of 1 themes.

To immediately activate the theme upon installation, please attach the --activate flag:

[user@server ~]$ wp theme install twentytwenty --activate
Installing Twenty Twenty (1.2)
Downloading installation package from https://downloads.wordpress.org/theme/twentytwenty.1.2.zip...
Using cached file '/home/user/.wp-cli/cache/theme/twentytwenty-1.2.zip'...
Unpacking the package...
Installing the theme...
Theme installed successfully.
Activating 'twentytwenty'...
Success: Switched to 'Twenty Twenty' theme.
Success: Installed 1 of 1 themes.

The "install" flag is so powerful it can also accept URLs or archives as parameters. This is useful if you have purchased a theme and have uploaded the archived files on your account, or you simply do not remember the exact theme's name. You can look it up and find its URL in the WordPress themes repository and then provide this parameter to the command. Here are some examples:

[user@server ~]$ wp theme install https://downloads.wordpress.org/theme/twentytwenty.1.2.zip
Downloading installation package from https://downloads.wordpress.org/theme/twentytwenty.1.2.zip...
Unpacking the package...
Installing the theme...
Theme installed successfully.
Success: Installed 1 of 1 themes.
[user@server ~]$ wp theme install twentytwenty.1.2.zip
Unpacking the package...
Installing the theme...
Theme installed successfully.
Success: Installed 1 of 1 themes.

List

To list your themes, please execute the command:

[user@server ~]$ wp theme list
+-----------------+----------+--------+---------+
| name | status | update | version |
+-----------------+----------+--------+---------+
| twentynineteen | active | none | 1.5 |
| twentyseventeen | inactive | none | 2.3 |
| twentysixteen | inactive | none | 2.1 |
| twentytwenty | inactive | none | 1.2 |
+-----------------+----------+--------+---------+

Activate

To activate a specific theme from a list, please type in the following:

[user@server ~]$ wp theme activate twentytwenty
Success: Switched to 'Twenty Twenty' theme.

This will deactivate the current theme and will enable the one you defined within the command.

Delete

To delete a theme which you no longer need, please type in the command:

[user@server ~]$ wp theme delete twentysixteen
Deleted 'twentysixteen' theme.
Success: Deleted 1 of 1 themes.

Day-to-day tasks you can perform using WP-CLI

These include things such as showing and changing the domain name you are using WordPress with and creating database backups.

Fetch the exact URL your website is currently using (including HTTP or HTTPS and www or non-www):

[user@server ~]$ wp option get home
http://test.hostarmada.com

Export your database

[user@server ~]$ wp db export
Success: Exported to 'user_wptest-2020-04-15-47a0755.sql'.

You may also set a custom name for your exported database like this:

[user@server ~]$ wp db export database_backup-04.15.2020.sql
Success: Exported to database_backup-04.15.2020.sql'.

The above commands follow up perfectly to the following option.

The search-replace WP-CLI option

ALWAYS back up your database before doing this. Here is the standard syntax you should follow:

wp search-replace 'current_url' 'new_url'

Here is an example:

[user@server ~]$ wp search-replace 'http://test.hostarmada.com' 'http://test.hostarmada.net'
+------------------+-----------------------+--------------+------+
| Table | Column | Replacements | Type |
+------------------+-----------------------+--------------+------+
| wp_commentmeta | meta_key | 0 | SQL |
| wp_commentmeta | meta_value | 0 | SQL |
| wp_comments | comment_author | 0 | SQL |
| wp_comments | comment_author_email | 0 | SQL |
| wp_comments | comment_author_url | 0 | SQL |
| wp_comments | comment_author_IP | 0 | SQL |
| wp_comments | comment_content | 0 | SQL |
| wp_comments | comment_approved | 0 | SQL |
| wp_comments | comment_agent | 0 | SQL |
| wp_comments | comment_type | 0 | SQL |
| wp_links | link_url | 0 | SQL |
| wp_links | link_name | 0 | SQL |
| wp_links | link_image | 0 | SQL |
| wp_links | link_target | 0 | SQL |
| wp_links | link_description | 0 | SQL |
| wp_links | link_visible | 0 | SQL |
| wp_links | link_rel | 0 | SQL |
| wp_links | link_notes | 0 | SQL |
| wp_links | link_rss | 0 | SQL |
| wp_options | option_name | 0 | SQL |
| wp_options | option_value | 2 | PHP |
| wp_options | autoload | 0 | SQL |
| wp_postmeta | meta_key | 0 | SQL |
| wp_postmeta | meta_value | 0 | SQL |
| wp_posts | post_content | 2 | SQL |
| wp_posts | post_title | 0 | SQL |
| wp_posts | post_excerpt | 0 | SQL |
| wp_posts | post_status | 0 | SQL |
| wp_posts | comment_status | 0 | SQL |
| wp_posts | ping_status | 0 | SQL |
| wp_posts | post_password | 0 | SQL |
| wp_posts | post_name | 0 | SQL |
| wp_posts | to_ping | 0 | SQL |
| wp_posts | pinged | 0 | SQL |
| wp_posts | post_content_filtered | 0 | SQL |
| wp_posts | guid | 3 | SQL |
| wp_posts | post_type | 0 | SQL |
| wp_posts | post_mime_type | 0 | SQL |
| wp_term_taxonomy | taxonomy | 0 | SQL |
| wp_term_taxonomy | description | 0 | SQL |
| wp_termmeta | meta_key | 0 | SQL |
| wp_termmeta | meta_value | 0 | SQL |
| wp_terms | name | 0 | SQL |
| wp_terms | slug | 0 | SQL |
| wp_usermeta | meta_key | 0 | SQL |
| wp_usermeta | meta_value | 0 | PHP |
| wp_users | user_login | 0 | SQL |
| wp_users | user_nicename | 0 | SQL |
| wp_users | user_email | 0 | SQL |
| wp_users | user_url | 1 | SQL |
| wp_users | user_activation_key | 0 | SQL |
| wp_users | display_name | 0 | SQL |
+------------------+-----------------------+--------------+------+
Success: Made 8 replacements.

There is a flag allowing you to perform a "dry run" that will simulate the replacement process, showing you exactly how many replacements the tool it aims to complete. To do so, please add the --dry-run flag at the end, as such:

[user@server ~]$ wp search-replace 'http://test.hostarmada.com' 'http://test.hostarmada.net' --dry-run

Clear your Object Cache

WP-CLI has a useful option allowing you to clear the WP Object Cache. This can come in handy if your WordPress is currently not utilizing a caching plugin. To flush your cache, use the command:

[user@server ~]$ wp cache flush
Success: The cache was flushed.

If you line up the 4 WP-CLI options we provided in the above paragraphs and you follow them, as per our instructions, you can easily change the URL of your website!

Manipulate tasks related to your WordPress users

All these tasks are performed using the "wp user" command. You may do the following:

  • List users Add users
  • Delete Users
  • Reset Passwords for users
  • Change user login
  • Change admin emails for users
  • Set or modify roles

In the next paragraphs, you will find examples of all the aforementioned tasks.

List users

To list your users, please type in the following:

[user@server ~]$  wp user list
+----+------------+--------------+------------------------+---------------------+---------------+
| ID | user_login | display_name | user_email | user_registered | roles |
+----+------------+--------------+------------------------+---------------------+---------------+
| 1 | admin | admin | [email protected] | 2020-04-15 09:25:32 | administrator |
| 3 | hostarmada | hostarmada | [email protected] | 2020-04-15 10:20:12 | administrator |
| 2 | johndoe | johndoe | [email protected] | 2020-04-15 10:19:53 | administrator |
+----+------------+--------------+------------------------+---------------------+---------------+

Add a user

To add a user, please use the shown example beneath:

[user@server ~]$ wp user create admin2 [email protected] --role=administrator
Success: Created user 4.
Password: sRxlA^ad@iZWhZ#PfCt^$(A)

As you can see, this also assigns a very strong password for the user. You may define your own password by adding the --user_pass flag:

[user@server ~]$ wp user create admin2 [email protected] --role=administrator --user_pass='sRxlA^ad@iZWhZ#PfCt^$(A)'
Success: Created user 4.

Delete a user

To delete a user, please use the command:

[user@server ~]$  wp user delete admin2
--reassign parameter not passed. All associated posts will be deleted. Proceed? [y/n] y
Success: Removed user 4 from http://test.hostarmada.com.

You will be required to type in "y" for confirmation. You also have the option of reassigning all posts this user created using the --reassign flag, followed by the new user:

[user@server ~]$ wp user delete admin2 --reassign=admin
Success: Removed user 4 from http://test.hostarmada.com.

Reassigning posts will not require confirmation. You may also use IDs instead of names. If you scroll up a bit and check the output of the list command, the first column will contain the IDs of each user. You may use them in your user manipulation commands:

[user@server ~]$ wp user delete 4 --reassign=1
Success: Removed user 4 from http://test.hostarmada.com.

Change user password

The user modifier allows you to change a user's password. Here is how it's done:

[user@server ~]$ wp user update admin --user_pass=';];934jasmdkl[]'
Success: Updated user 1.

You may use the user's ID, as explained in user deletion examples:

[user@server ~]$ wp user update 1--user_pass=';];934jasmdkl[]'
Success: Updated user 1.

Change administrative email address of a user

To change the administrative email address of a user, please use the command below:

[user@server ~]$ wp user update admin [email protected]
Success: Updated user 1.

You may also use the ID of the user, as explained in the above two examples.

Change the role of a user

Changing the role is also something that you may perform using WP-CLI. The list of available roles are as follows:

  • administrator
  • editor
  • author
  • contributor
  • subscriber

These are the default ones. There are plugins or themes that may add additional ones that may not be altered with WP-CLI!

[user@server ~]$  wp user set-role 1 editor
Success: Added admin (1) to http://test.receptitenamama.com as editor.

Final Thoughts

The examples we provided in this article merely touch the surface of the WordPress command-line interface tool! They should be sufficient enough to give you a start in your long journey of becoming a guru in WP-CLI.

Was this article useful and on point?

Find out more about HostArmada entire range of optimized Web Hosting Services and take action today on improving your website Loading Speed, Security, and overall Stability!