Skip to content

WordPress migrations between environments — the db_url replacement tools compared

WordPress migrations between dev, staging, and production all hinge on URL rewrites in serialized data. The tools that do it correctly — WP-CLI, WP Migrate, Search Replace DB — and when to use each.

A WordPress agency moves a site from dev.example.com to staging.example.com to example.com. At each step, the database is full of URLs pointing at the old hostname, and many of them are inside serialized PHP arrays that a naive sed corrupts. There are three tools that do this correctly — WP-CLI’s built-in wp search-replace, the WP Migrate plugin family, and interconnect/it’s Search Replace DB. Each has a place; the wrong choice for the situation slows the migration or corrupts the data.

What’s happening

WordPress stores URLs in three kinds of places:

  1. Plain text columns — wp_options.option_value (often serialized but sometimes not), wp_posts.post_content, wp_postmeta.meta_value, wp_usermeta.meta_value.
  2. Serialized PHP arrays — the format encodes string length, so naive replacement corrupts arrays where the new string length differs from the old.
  3. JSON inside meta — Gutenberg blocks, ACF, page builders often serialize JSON inside meta fields. A URL inside a JSON inside a serialized array needs three layers of awareness.

The tools:

  • WP-CLI’s wp search-replace is built into WP-CLI. Handles serialized PHP natively. Works on tables and columns selectively. Free, no plugin install.
  • WP Migrate (paid plugin by Delicious Brains/WP Engine) — handles the same plus push/pull between environments, includes find-and-replace with regex, integrates with the WordPress dashboard. Useful for non-CLI workflows.
  • interconnect/it’s Search Replace DB — standalone PHP script you upload, browse-based UI. Pre-WP-CLI era tool, still works, useful when you can’t get a shell on the host.

For 95% of cases, WP-CLI’s wp search-replace is the right answer. The exceptions are when you need the dashboard UI (non-technical user doing the migration), can’t get SSH access (some shared hosts), or want a paid plugin’s deeper integration (multi-environment push/pull).

How to verify

For any candidate migration, count what you’re about to rewrite:

wp search-replace "https://old.example.com" "https://new.example.com" \
  --dry-run --report-changed-only

The --dry-run output lists how many rows in each table would change. If wp_postmeta shows 12,000 changes, those are mostly serialized data and you need the recursion. If wp_options shows 80, those are mostly plugin settings.

For the serialized question specifically:

# Check if a particular option is serialized
wp option get active_plugins --format=json   # serialized in DB, but WP unserializes for output
wp eval 'echo (is_serialized(get_option("active_plugins"))) ? "yes" : "no";'

For URL inventory before migration:

wp db query "SELECT COUNT(*) FROM wp_postmeta WHERE meta_value LIKE '%old.example.com%';"
wp db query "SELECT COUNT(*) FROM wp_options WHERE option_value LIKE '%old.example.com%';"

What to do

Use WP-CLI by default; reach for the others only when WP-CLI can’t do it.

  1. The default WP-CLI command:
wp search-replace "https://old.example.com" "https://new.example.com" \
  --recurse-objects \
  --skip-columns=guid \
  --all-tables-with-prefix \
  --report-changed-only \
  --dry-run

# Once the dry-run output looks right, drop --dry-run
wp search-replace "https://old.example.com" "https://new.example.com" \
  --recurse-objects --skip-columns=guid --all-tables-with-prefix
  • --recurse-objects walks into serialized arrays and objects. This is the magic.
  • --skip-columns=guid is required — guid is WordPress’s permanent identifier and shouldn’t change.
  • --all-tables-with-prefix includes plugin-created tables that share the wp_ prefix.
  • --report-changed-only keeps the output to lines where something changed.
  1. For multi-step replacements (HTTP to HTTPS, plus domain change):
wp search-replace "http://old.example.com" "https://new.example.com" \
  --recurse-objects --skip-columns=guid --all-tables-with-prefix

wp search-replace "https://old.example.com" "https://new.example.com" \
  --recurse-objects --skip-columns=guid --all-tables-with-prefix

Two passes, not one regex. Regex with --regex works but is slower and more error-prone on serialized data.

  1. For specific tables only (when you only want to touch postmeta, not posts):
wp search-replace "old" "new" wp_postmeta wp_options \
  --recurse-objects --report-changed-only
  1. For JSON-inside-meta (Gutenberg block attributes that store URLs as JSON strings inside serialized meta): --recurse-objects handles the serialized layer, but if the JSON itself escapes the URL (\/\/old.example.com), you need a second pass with the escaped form:
wp search-replace 'old.example.com' 'new.example.com' \
  --recurse-objects --skip-columns=guid --all-tables-with-prefix

wp search-replace '\/\/old.example.com' '\/\/new.example.com' \
  --recurse-objects --skip-columns=guid --all-tables-with-prefix
  1. For WP Migrate (the plugin) workflow:

The plugin’s “Migrate” → “Pull” or “Push” flow handles URL rewriting automatically. Two sites with the WP Migrate plugin can sync database, media, and theme files between them. The use case is non-technical users doing routine refreshes; the cost is a paid license.

  1. For Search Replace DB (the standalone script):

Upload Search-Replace-DB-master/ to the web root, browse to it, fill in old/new strings, click Live Run. Delete the script immediately after — it’s a security hole if left in place. Use only when SSH access isn’t available.

  1. After any of these, flush rewrites and caches:
wp rewrite flush
wp cache flush
wp transient delete --all
  1. Verify:
wp db query "SELECT COUNT(*) FROM wp_options WHERE option_value LIKE '%old.example.com%';"
# Should return 0

For the deeper serialized-data discussion and edge cases, see WordPress search-replace for serialized data.

Stack Harbor’s enterprise WordPress operations practice runs these migrations daily — including the surprise edge cases with page builders, ACF flexible content, and Gutenberg blocks. If a site has stalled mid-migration because the URLs aren’t fully rewriting, we can sort it.