Web hosting

WP-CLI

Definition

WP-CLI is the official command-line interface for WordPress. From a terminal, often over SSH, it can do almost everything the admin area offers, and more: install and update core, plugins and themes, manage users, export the database, replace URLs or verify file integrity.

WP-CLI, Web hosting

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-checksums and wp 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-run to check before writing. It's the go-to tool for moving from a staging site to production.
  • wp cron event list and wp 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-name gives 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-plugins and --skip-themes when 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.

Further reading

  • Article Has my WordPress site been hacked? Signs, checks and tools
  • Article Cleaning up a hacked WordPress site: the step-by-step procedure

Sources

  1. WordPress.org: WP-CLI (external link, opens in a new tab)
  2. WP-CLI Handbook: Installing (external link, opens in a new tab)
  3. WP-CLI Commands: wp search-replace (external link, opens in a new tab)
  4. WP-CLI Commands: wp cron event run (external link, opens in a new tab)

All definitions

Start free, go Pro when you’re ready

The free version already hides WordPress and moves the login page. Pro adds aliases, two-factor authentication and monitoring.