Welcome to WP-CLI!

I have limited experience with wp-cli but I know it’s a really quick and convenient way to manage WordPress installations. And recently I’ve been using ssh to to manage my sites more often, so I thought it would be a good time to investigate it more deeply.

I’m hosting this site on Dreamhost.com (my host since 2009), and I recently went through their One-Click install to quickly add WordPress here. But, strange thing is, I didn’t receive an email from them with my username/password combination – so I had no way to login (at first). 

After a quick Google search, I noticed that Dreamhost has a page about resetting your password – including a section on using wp-cli

I have limited experience with wp-cli but I know it’s a really quick and convenient way to manage WordPress installations. And recently I’ve been using ssh to to manage my sites more often, so I thought it would be a good time to investigate it more deeply.

Here’s a list of the very first commands I’m running on my new WP install – as well as some I intend to put to greater use in the future.

Login

First of all, we need to ssh into the server and access the site.

$ ssh username@site.com
$ cd site.com/wordpress

Change user password

$ wp user list
+----+-------------+---------------+------------------+---------------------+---------------+
| ID | user_login | display_name | user_email | user_registered | roles |
+----+-------------+---------------+------------------+---------------------+---------------+
| 1 | myusername | myusername | user@example.com | 2018-03-17 11:14:28 | administrator |
+----+-------------+---------------+------------------+---------------------+---------------+
$ wp user update 1 --user_pass=NewerBetterStrongerPassword123$
Success: Updated user 1

Create an alias to check for updates

One of the coolest things I found was that you can create an alias to run a series of wp-cli commands one after the other. The following command will check for WP core, plugin, and theme updates at once.

$ alias check-all='wp core check-update && wp plugin list --update=available && wp theme list --update-available'
$ check-all
Success: WordPress is at the latest version.
+------+--------+--------+---------+
| name | status | update | version |
+------+--------+--------+---------+
+------+--------+--------+---------+
+-----------------+----------+--------+---------+
| name | status | update | version |
+-----------------+----------+--------+---------+
| twentyfifteen | inactive | none | 2.0 |
| twentyseventeen | active | none | 1.7 |
| twentysixteen | inactive | none | 1.5 |
+-----------------+----------+--------+---------+

Update and manage core, plugins, & themes

While I could probably create another alias to update everything at once, I haven’t yet because I still like to do some things step-by-step. So, here are the three commands needed to update or manage WP core, plugins, and themes:

$ wp core update
$ wp core update --version=4.7.1

$ wp plugin update --all
$ wp plugin status
$ wp plugin install plugin-name
$ wp plugin activate plugin-name
$ wp plugin deactivate plugin-name
$ wp plugin delete plugin-name

$ wp theme update --all
$ wp theme status theme-name
$ wp theme install theme-name --activate

Many of the plugin and theme commands are quite similar and you can do certain things like stringing together some of them (wp theme install theme-name --activate).

Check your config file or databases

A couple more useful commands are to check out your wp-config file’s constants and globals, and work with your database:

$ wp config get

$ wp db size --tables
$ wp db optimize
$ wp db repair
$ wp db export filename.sql

Clean up post revisions

Sometimes you just need to clean up your post revisions, especially if your site is old or has loads of them. But first, you’ll need to install an additional wp-cli package.

$ wp package install trepmal/wp-revisions-cli
$ wp revisions clean

Or, for specific posts and/or dates:

$ wp revisions list --post_id=ID
$ wp revisions clean --post_id=ID --before-date="YYYY-MM-DD"

Remove all spam comments

This one looks to be a great help, especially for big installations that get a lot of spam. But there are quite a few other commands to help manage comments.

$ wp comment delete $(wp comment list --status=spam --format=ids)

Resources