WP-CLI is the official command-line interface for WordPress, and once you start using it you will wonder how you ever managed without it. Tasks that take ten clicks in the admin panel — updating all plugins, regenerating thumbnails, importing a database dump, searching and replacing URLs after a domain change — are reduced to a single command you can run over SSH in seconds. WP-CLI is pre-installed on most managed WordPress hosts and can be installed on any Linux or macOS server in under a minute. The most transformative command is wp search-replace, which handles serialised PHP data correctly (unlike a raw MySQL search-replace that breaks serialised arrays). wp media regenerate rebuilds all image sizes after you change the registered sizes in your theme — a process that would otherwise require a plugin. wp plugin update --all and wp core update let you manage updates in bulk from a deployment script. Combining WP-CLI with the bash backup script and core Linux commands gives you a complete server-side toolkit. You can also write custom WP-CLI commands as classes registered with WP_CLI::add_command(), which is ideal for custom data migrations or site-health checks that you run regularly. The reference below covers the twenty commands I reach for most often.
Problem: You need to perform bulk WordPress operations (search-replace after migration, mass plugin update, thumbnail regeneration) efficiently from the command line.
Solution: Use the following WP-CLI commands via SSH:
# ── Install WP-CLI ──────────── curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar chmod +x wp-cli.phar && sudo mv wp-cli.phar /usr/local/bin/wp wp --info # verify installation # ── Core & updates ──────────── wp core update # update WordPress core wp plugin update --all # update all plugins wp theme update --all # update all themes wp core verify-checksums # detect tampered core files # ── Search & replace (handles serialised data correctly) ──────────── wp search-replace 'http://old-domain.com' 'https://new-domain.com' --dry-run wp search-replace 'http://old-domain.com' 'https://new-domain.com' # ── Database ──────────── wp db export backup-$(date +%Y%m%d).sql # export DB wp db import backup.sql # import DB wp db optimize # optimise all tables wp db query "SELECT COUNT(*) FROM wp_posts WHERE post_status='publish';" # ── Media ──────────── wp media regenerate --yes # regenerate all thumbnails wp media import /path/to/image.jpg --title="My Image" # ── Users ──────────── wp user create editor editor@example.com --role=editor --send-email wp user update 1 --user_pass='NewSecurePass!2021' wp user list --role=administrator # ── Cache ──────────── wp cache flush # clear object cache wp transient delete --all # delete all transients # ── Cron ──────────── wp cron event list # show scheduled cron events wp cron event run helloadmin_send_followup # manually trigger an event
NOTE: Always run wp search-replace --dry-run first to preview the changes before committing. The --dry-run flag outputs how many rows would be changed without modifying the database. On multisite installations, add the --network flag to run the command across all sites. When running WP-CLI as root, add the --allow-root flag; however, it is safer to run it as the web server user (sudo -u www-data wp ...) to avoid file ownership issues.