Skip to main content

WP-CLI Commands: Database Search-Replace and Admin Users

WordPress · 21.01.2025 · 4 min read
Illustration for “WP-CLI Commands: Database Search-Replace and Admin Users”

What WP-CLI Is

WP-CLI is the official command-line tool for managing WordPress. It lets you update plugins, manage users, run search-and-replace across the database and much more without ever logging in to wp-admin.

Installing WP-CLI

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
wp --info
💡 Run WP-CLI as the site user, not as root: sudo -u www-data wp ...

Essential Commands

CommandDescription
wp core updateUpdate WordPress
wp plugin update --allUpdate all plugins
wp theme listList themes
wp user listList users
wp db export backup.sqlExport the database
wp cache flushFlush the cache

Search and Replace in the Database

# Migrating from HTTP to HTTPS
wp search-replace 'http://example.com' 'https://example.com' --all-tables

# Changing the domain
wp search-replace 'old-domain.com' 'new-domain.com' --all-tables --dry-run
⚠️ Always run --dry-run before the real replacement — it reports how many rows would change without changing anything.

Creating an Administrator Account

wp user create admin2 admin2@example.com   --role=administrator   --user_pass=StrongPass123!

Diagnosing WP-CLI errors

WP-CLI runs commands under the PHP CLI SAPI, not the web server, so some errors are specific to the command line itself.

Error textCauseWhat to do
Error: This does not seem to be a WordPress installationThe command was run outside the site root, so WP-CLI cannot find wp-load.phpChange into the directory with wp-config.php, or pass --path=/var/www/site
Error: Because "wpdb" is a global, we can not run this commandCode inside wp eval runs before WordPress has fully bootstrappedUse wp eval-file and wait for the init hook inside the file
PHP Fatal error: Allowed memory size exhaustedThe CLI SAPI memory_limit in php.ini is lower than what thousands of search-replace changes needTemporarily raise the CLI memory_limit in a separate php.ini, or with the --require flag
Error establishing a database connection during wp core update-dbwp-config.php has the wrong DB_HOST or DB_USER after moving the site to a new serverCheck the connection constants and re-run the command

If the error is specifically about changing the domain, see the separate article on changing a WordPress domain.

When WP-CLI is not the right tool

  • Hosting without SSH access (many shared plans) — there is physically nowhere to run WP-CLI
  • Bulk content edits in the Gutenberg block editor — a careless search-replace can break the serialized JSON attributes inside blocks
  • A multisite network with dozens of subdomains — every command needs the --url flag for the specific site, otherwise it hits the wrong table
  • Changes that need a visual check — design, layout, block placement — the CLI shows none of that; you need a browser and staging
  • Managing access rights and roles at the level of individual posts — WP-CLI manages roles as a whole, and fine-grained capability control on one post needs code or a plugin

It is also worth remembering file system permissions: by default, WP-CLI runs as whichever user invoked it. If you run a command as root on a site where the web server runs as www-data, the new or changed files end up not writable by the web server — plugins stop updating through the wp-admin interface.

Backing up and profiling before changes

Before any bulk operation with WP-CLI, take a database snapshot — restoring it is faster than untangling the consequences by hand.

wp db export --add-drop-table backup-before-migrate.sql
wp profile stage --url=example.com
wp transient delete --all

For the backup process itself, see the article on WordPress backup. Cleaning up unused tables and transients after bulk migrations should happen right after the checksum check, not be put off for later.

How to check that a command actually worked

  • After wp core update, verify the core checksums against the official repository
  • After updating plugins, verify the checksums of each plugin separately
  • After search-replace with --dry-run, compare the reported row count with the real run without the flag
  • After any migration, open the site in a browser and confirm no old domain remains in the links
  • Check the command's exit code with echo $? right after it runs — 0 means success, any other number means an error, even if the console output looked fine
wp core verify-checksums
wp plugin verify-checksums --all
echo $?  # 0 — checksums matched

If the command failed while updating WordPress core itself, cross-check it against the general VPS migration plan in the article on moving WordPress to a VPS.

Was this article helpful?
← Back to Knowledge Base Ask Support