WP-CLI Cheatsheet

wp cli commands that I use often when dealing with WordPress sites

If you work with WordPress, one of the handiest tools you can add to your toolbelt is WP-CLI. It’s like a swiss army knife, especially when you’re having trouble accessing the web interface, if you have SSH access you can do so much with it.

The following are examples of how I use it in my day to day tasks.

Search and Replace

Sometimes, when you’re going from a dev url to a production instance, you need to search and replace certain text in the database to now point to the production instance. This is where you can use the search-replace command of the wp-cli utility.

Take out the —dry-run option when you actually want to make the changes

 wp search-replace --dry-run --skip-columns=guid --report-changed-only 'lookfor' 'replacewith'

Add a user to an install

wp user create username [email protected] --role=administrator

Get a list of users

wp user list 

+----+---------------+----------------+---------------------------------+---------------------+---------------+
| ID | user_login    | display_name   | user_email                      | user_registered     | roles         |
+----+---------------+----------------+---------------------------------+---------------------+---------------+
| 1  | notadmin         | First Lastn | [email protected]           | 2012-09-24 12:05:52 | administrator |
| 51 | someotheruser    | F Lastname  | [email protected]        | 2021-09-22 16:25:38 | administrator |
+----+---------------+----------------+---------------------------------+---------------------+---------------+

Reset a user’s password

Once you see the userlist above, the userid is the first column you see.. so use that to change the password

wp user update userid --user_pass=somenewpassword

Change the home and siteurl of the site

Say you started a site out as https://dev.example.com, but now you’re ready to go live to example.com, you can override this option in your wp-config.php or you can update the options table with this new

First confirm what the option is that WordPress knows about.

wp option get home 
https://dev.example.com 

wp option get siteurl 
https://dev.example.com 

Now we can change it by issuing the following command.

wp option update home 'http://example.com'
wp option update siteurl 'http://example.com'

Update all plugins

 wp plugin update --all