The official WP-CLI page sums it up: anything you can do in the WordPress admin, you can do from the terminal. The benefit isn't just speed. A command runs identically on ten sites, can be saved in a script, launched by a scheduled task, and keeps working even when the admin area is unreachable, for example after a plugin crashes the site.
Installation
WP-CLI is a single PHP archive file (phar). The official installation guide says it needs PHP 7.2.24 or later, with no requirements beyond those of WordPress:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
wp cli update then keeps it up to date. Many hosts already provide it: try wp --info in an SSH session before installing it. Run it as the user who owns the site's files, not as root, so you don't create files that PHP can no longer modify.
The most useful commands
wp core update,wp plugin update --all,wp theme update --all: updates, with a report for each item.wp core verify-checksumsandwp plugin verify-checksums --all: compare core files and plugins from the official directory against the published checksums, to spot a modified or added file.wp user list --role=administrator: the list of administrators, worth checking after an incident; our article on roles and least privilege explains what to do with it.wp db export: a database export, the first building block of a backup.wp search-replace: replaces a URL throughout the database while handling serialized data, with--dry-runto check before writing. It's the go-to tool for moving from a staging site to production.wp cron event listandwp cron event run --due-now: list and run scheduled tasks, useful for replacing the visit-triggered WP-Cron.
For WordPress
- Diagnosing a hack: checking checksums, the list of administrators and the list of active plugins are the first steps in our guide is my site hacked?, and the procedure to clean up a hacked site relies on the same commands.
- Deactivating a plugin that locks you out:
wp plugin deactivate plugin-namegives you back control without going through the admin area. - Automating updates and exports in a script run by cron, while keeping a log of each run.
- Adding
--skip-pluginsand--skip-themeswhen the site is suspect or broken: otherwise WP-CLI loads the plugins' code, including any malicious code.
Limitations
WP-CLI acts with the rights of the system user, not those of a WordPress account: anyone with SSH access to the account can change everything. Protect that access accordingly. Checksum verification doesn't cover files in the uploads folder, or premium plugins that aren't in the official directory.