Help Docs Content Management Systems (CMS) WordPress Overview What is WP-CLI?

What is WP-CLI?

WP-CLI is a specialized command line interface for WordPress, empowering users to interact with sites and databases via commands, offering enhanced control over operations.

WP-CLI is a command line interface made specifically for WordPress. WP-CLI is a powerful tool to help you connect with your sites and databases through the command line and give you more control over the functions performed. 

Because WP-CLI relies on the wp-config.php to query your database, it is important to take a backup of your database before making any changes to your site. For help with creating a backup, see Taking a Manual Backup with FTP/SFTP.

Common Commands

There are a few common commands you should know while working with WP-CLI. These commands will help you troubleshoot and manage your WordPress sites. You can find a list of all the commands for WP-CLI at https://developer.wordpress.org/cli/commands or use the help command for a list of commands that are available while in WP-CLI:

wp help

There are also subcommand help pages for information on a specific subcommand. For example, if you want to know more about the plugin command, you can enter the following command in your WP-CLI:

wp help plugin

Listed below are the common commands that are most often used with WP-CLI:

Check WordPress Users
Update WordPress User Password
Search and Replace
Check WordPress Version
Check Core Files for Modifications 

When using any command that will potentially change the database, use the flag –dry-run at the end of your command so you can view the output and verify that it is correct before making the change live.

Check WordPress Users

In addition to backing up your database, you will want to view the WordPress users on your site and verify that no one else is making changes at the same time you are to avoid potential issues and data loss. You can list the users using the following command. 

wp user list

This will produce a list of the user, email address, registered date and their role.

+----+---------------+---------------+----------------------+---------------------+---------------+
| ID | user_login    | display_name  | user_email           | user_registered     | roles         |
+----+---------------+---------------+----------------------+---------------------+---------------+
| 1  | lwtrainingmwp | lwtrainingmwp | sampleuser@liquidweb.com | 2016-10-14 20:40:55 | administrator |
+----+---------------+---------------+----------------------+---------------------+---------------+

Update WordPress User Password

Use this command to update any user password in WordPress. 

Changing the password within WP-CLI will store the new password in the history and email the user that their password has been changed.
wp user update $USERNAME --user_pass=$PASSWORD

Search and Replace

You can run a search and replace on your database, this comes in handy if you need to change slug names or need to find a specific section of your database to correct an error in multiple lines. 

To run a search and replace on the database, use the following command:

wp search-replace 'http://example.dev' 'http://example.com' --all-tables --skip-columns=guid --skip-themes --skip-plugins --dry-run

Note that I have three specific flags at the end of the command. These flags are important and help make sure that you don’t modify items in the database that should never be modified. Let’s take a look at these flags in depth:

  • –dry-run: this flag gives you a preview of the output prior to running a command live. This helps you to troubleshoot any issues you see without making any permanent changes to the database.
  • –skip-columns=guidAlways run any command on the database with this flag. See the warning below for more information on this flag.
  • –skip-themes: This will skip modifying the themes within the database. If you need to modify a theme, it is best to do from your WordPress wp-admin home page, rather than directly from the database.
  • –skip-plugins: Skip modifying the plugins section of your database. If you need to modify a plugin, it is best to do from your WordPress wp-admin home page, rather than directly from the database.

Once you’ve verified that there are no errors or issues, run the command a second time without the –dry-run flag at the end.

Example output: In this example, I’m converting all URLs in the database away from staging to live. It will list all tables and columns in which the change was made and a tally of each change. A total list of changes will show at the bottom.

If you are replacing URLs, don’t forget your trailing slash! Always use –skip-columns=guid so that you do not change the contents of the GUID field. See the official WordPress notice about updating guid here: https://codex.wordpress.org/Changing_The_Site_URL#Important_GUID_Note.
sample@samplesite.com [~/public_html]# wp search-replace 'http://staging.samplesite.com' 'http://samplesite.com' --skip-columns=guid --skip-themes --skip-plugins --dry-run
+--------------------+-----------------------+--------------+------+
| Table              | Column                | Replacements | Type |
+--------------------+-----------------------+--------------+------+
| wpej_commentmeta   | meta_key              | 0            | SQL  |
| wpej_commentmeta   | meta_value            | 0            | SQL  |
| wpej_comments      | comment_author        | 0            | SQL  |
| wpej_comments      | comment_author_email  | 0            | SQL  |
| wpej_comments      | comment_author_url    | 0            | SQL  |
| wpej_comments      | comment_author_IP     | 0            | SQL  |
| wpej_comments      | comment_content       | 0            | SQL  |
| wpej_comments      | comment_approved      | 0            | SQL  |
| wpej_comments      | comment_agent         | 0            | SQL  |
| wpej_comments      | comment_type          | 0            | SQL  |
| wpej_links         | link_url              | 0            | SQL  |
| wpej_links         | link_name             | 0            | SQL  |
| wpej_links         | link_image            | 0            | SQL  |
| wpej_links         | link_target           | 0            | SQL  |
| wpej_links         | link_description      | 0            | SQL  |
| wpej_links         | link_visible          | 0            | SQL  |
| wpej_links         | link_rel              | 0            | SQL  |
| wpej_links         | link_notes            | 0            | SQL  |
| wpej_links         | link_rss              | 0            | SQL  |
| wpej_options       | option_name           | 0            | SQL  |
| wpej_options       | option_value          | 2            | PHP  |
| wpej_options       | autoload              | 0            | SQL  |
| wpej_postmeta      | meta_key              | 0            | SQL  |
| wpej_postmeta      | meta_value            | 0            | SQL  |
| wpej_posts         | post_content          | 3            | SQL  |
| wpej_posts         | post_title            | 0            | SQL  |
| wpej_posts         | post_excerpt          | 0            | SQL  |
| wpej_posts         | post_status           | 0            | SQL  |
| wpej_posts         | comment_status        | 0            | SQL  |
| wpej_posts         | ping_status           | 0            | SQL  |
| wpej_posts         | post_password         | 0            | SQL  |
| wpej_posts         | post_name             | 0            | SQL  |
| wpej_posts         | to_ping               | 0            | SQL  |
| wpej_posts         | pinged                | 0            | SQL  |
| wpej_posts         | post_content_filtered | 0            | SQL  |
| wpej_posts         | guid                  | 13           | SQL  |
| wpej_posts         | post_type             | 0            | SQL  |
| wpej_posts         | post_mime_type        | 0            | SQL  |
| wpej_term_taxonomy | taxonomy              | 0            | SQL  |
| wpej_term_taxonomy | description           | 0            | SQL  |
| wpej_termmeta      | meta_key              | 0            | SQL  |
| wpej_termmeta      | meta_value            | 0            | SQL  |
| wpej_terms         | name                  | 0            | SQL  |
| wpej_terms         | slug                  | 0            | SQL  |
| wpej_usermeta      | meta_key              | 0            | SQL  |
| wpej_usermeta      | meta_value            | 0            | PHP  |
| wpej_users         | user_login            | 0            | SQL  |
| wpej_users         | user_nicename         | 0            | SQL  |
| wpej_users         | user_email            | 1            | SQL  |
| wpej_users         | user_url              | 1            | SQL  |
| wpej_users         | user_activation_key   | 0            | SQL  |
| wpej_users         | display_name          | 0            | SQL  |
+--------------------+-----------------------+--------------+------+
Success: 20 replacements to be made.

Check WordPress Version

One of the first troubleshooting actions to take when working with WordPress is to check the version. If you are running an out-of-date version, you are more vulnerable to malicious activity and for plugins and themes to be incompatible.

In order to check your version, make sure you are in the html folder. To do this, run the following command:
cd html

Run the following command to check your version:

wp core version

The output should give you the current version number:

s12@d25e08a78c59:~/html$ wp core version
4.8.2

Check Core Files for Modifications

Once you’ve checked your version, you may want to check for modifications to the core files. When you run this command, it will check the WordPress core files against wordpress.org :

wp core verify-checksums

If there are no modifications, you will see the following output:

Success: WordPress install verifies against checksums.

You can also check specifying a version:

wp core verify-checksums --version=$VERSIONNUMBER

Next, check out managing your users in our article Creating and Modifying Users With WP-CLI.

Was this article helpful?