WP-CLI is the official command-line tool for WordPress. Anything you’d click through in wp-admin — updates, plugins, users, settings — you can do in seconds over SSH, and script across many sites. On a VPS it’s indispensable.

Install

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 --info

Update it later with sudo wp cli update.

Run it as the right user

WP-CLI acts on the WordPress install in the current directory (or --path=). Run it as the user that owns the files — usually www-data — so new files get the right owner:

cd /var/www/example.com
sudo -u www-data wp plugin list

Running as root triggers a warning and may leave root-owned files that WordPress can’t modify. In the examples below, prefix with sudo -u www-data as needed.

Core

wp core version
wp core check-update
wp core update
wp core update-db                 # database upgrade after a core update
wp core verify-checksums          # detect modified core files (malware check)
wp core download --locale=en_US   # fresh copy of WordPress

verify-checksums compares every core file with the official release — an excellent first step when you suspect a hack.

Plugins

wp plugin list
wp plugin list --update=available
wp plugin install redis-cache --activate
wp plugin activate classic-editor
wp plugin deactivate --all          # troubleshooting a white screen
wp plugin update --all
wp plugin delete hello
wp plugin verify-checksums --all    # for plugins hosted on wordpress.org

Locked out of wp-admin by a broken plugin? wp plugin deactivate plugin-name fixes it without FTP.

Themes

wp theme list
wp theme install astra --activate
wp theme activate twentytwentyfive
wp theme update --all
wp theme delete twentytwentythree

Users

wp user list
wp user create editor1 [email protected] --role=editor --prompt=user_pass
wp user update admin --user_pass="New-Strong-Password"
wp user set-role 5 subscriber
wp user delete 7 --reassign=1

Resetting a forgotten admin password is a one-liner. See resetting a WordPress admin password.

Options and configuration

wp option get siteurl
wp option update blogname "My Site"
wp option update home 'https://example.com'
wp option update siteurl 'https://example.com'

wp config list
wp config set WP_MEMORY_LIMIT 256M
wp config set DISABLE_WP_CRON true --raw
wp config shuffle-salts           # log everyone out by rotating keys

--raw writes true as a boolean instead of the string 'true'.

Database

wp db export backup-$(date +%F).sql
wp db import backup.sql
wp db optimize
wp db size --tables
wp db query "SELECT COUNT(*) FROM wp_posts WHERE post_status='publish'"
wp db cli                          # open a MySQL shell with the site's credentials

Search and replace (domain changes, HTTPS)

wp search-replace 'http://old.example.com' 'https://example.com' --all-tables --dry-run
wp search-replace 'http://old.example.com' 'https://example.com' --all-tables

Unlike a raw SQL REPLACE, WP-CLI correctly handles serialized PHP data, where string lengths are stored alongside values. Always --dry-run first and back up the database. More in WordPress search and replace.

Posts and media

wp post list --post_type=page --fields=ID,post_title,post_status
wp post delete $(wp post list --post_status=trash --format=ids) --force
wp post delete $(wp post list --post_type=revision --format=ids) --force   # clean revisions
wp media regenerate --yes          # rebuild thumbnails after changing image sizes
wp media import ~/photos/*.jpg

Cron

wp cron event list
wp cron event run --due-now
wp cron test

With DISABLE_WP_CRON, schedule wp cron event run --due-now from the system crontab every five minutes — see WordPress real cron.

Cache and transients

wp cache flush
wp transient delete --expired
wp transient delete --all
wp rewrite flush                   # regenerate permalink rules

Maintenance mode

wp maintenance-mode activate
wp maintenance-mode deactivate

Multisite

wp site list
wp plugin update --all --url=shop.example.com
wp site list --field=url | xargs -I{} wp --url={} cache flush

Script it across many sites

for site in /var/www/*/; do
  echo "== $site"
  sudo -u www-data wp --path="$site" core update --minor
  sudo -u www-data wp --path="$site" plugin update --all
done

Remote sites with aliases

~/.wp-cli/config.yml:

@prod:
  ssh: [email protected]/var/www/example.com
@staging:
  ssh: [email protected]/var/www/staging.example.com
wp @prod plugin list
wp @staging db export - > staging.sql

Help

wp help
wp help plugin install

New WordPress server? Start with installing WordPress on Ubuntu with Nginx. All WordPress VPS plans include SSH access, so WP-CLI works out of the box.

  • WordPress
  • WP-CLI
  • Command Line