Essential WP-CLI commands every WordPress developer should know

Essential WP-CLI commands for WordPress developers

WP-CLI is the official command-line interface for WordPress. Tasks that take ten clicks in the admin (updating all plugins, regenerating thumbnails, importing a database, replacing URLs after a domain change) become one command over SSH. Most managed hosts have it installed; on any Linux or macOS server it takes a minute to install.

Problem: Do bulk WordPress operations (search-replace after a migration, updates, thumbnail regeneration) quickly from the command line.

Solution: The WP-CLI commands I use most often. Run them in the WordPress folder or add --path=/var/www/html.

Install

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

Core and updates

wp core version
wp core update
wp plugin list --update=available
wp plugin update --all
wp theme update --all
wp core verify-checksums     # compare core files with the official ones
wp plugin verify-checksums --all

verify-checksums is a quick check after a hack: it lists the core and plugin files that differ from the originals on wordpress.org.

Search and replace

wp search-replace 'http://old-domain.com' 'https://new-domain.com' --dry-run
wp search-replace 'http://old-domain.com' 'https://new-domain.com' --skip-columns=guid

Unlike a plain SQL REPLACE(), it handles serialized data correctly. --dry-run only shows how many replacements would be made in each table.

Database

wp db export backup-$(date +%Y%m%d).sql
wp db import backup.sql
wp db query "SELECT COUNT(*) FROM wp_posts WHERE post_status = 'publish';"
wp db optimize

wp db query uses the credentials from wp-config.php, so you do not need to type the password. For regular backups see the bash backup script.

Media, users, cache, cron

wp media regenerate --yes                  # rebuild all image sizes
wp user list --role=administrator
wp user create editor editor@example.com --role=editor --send-email
wp user update 1 --prompt=user_pass        # asks for the new password
wp cache flush                             # object cache
wp transient delete --all
wp cron event list
wp cron event run --due-now
wp rewrite flush

--prompt=user_pass asks for the password instead of taking it as an argument, so it does not end up in the shell history.

Your own commands

For tasks you repeat (data migrations, reports), a class can become a command:

<?php
if ( defined( 'WP_CLI' ) && WP_CLI ) {
    WP_CLI::add_command( 'helloadmin stats', function () {
        $count = wp_count_posts()->publish;
        WP_CLI::success( "Published posts: $count" );
    } );
}

Then wp helloadmin stats runs it.

NOTE: Run WP-CLI as the user that owns the site files, for example sudo -u www-data wp plugin update --all. As root it refuses to run without --allow-root, and the files it creates (updates, uploads) would belong to root, which then breaks updates from the admin. On multisite add --url= for one site or --network where the command supports it.