You take a copy of production, change the domain in wp-config.php, and run sed -i 's/oldsite.com/newsite.com/g' across the SQL dump. The home page loads. Then a customer opens a page built with Elementor and the editor reports “the content area was modified outside of Elementor.” Half the widgets are gone. Meta_value rows that contained serialized arrays now have lengths that no longer match the strings they describe — and PHP’s unserialize() returns false instead of an array.
What’s happening
WordPress stores a lot of data as serialized PHP. The format encodes the length of each string in bytes:
s:11:"oldsite.com";
The 11 is the byte length of oldsite.com. If a sed pass replaces oldsite.com with newsite.com, the string changes but the 11 does not — and newsite.com is also 11 characters, so this exact case works by accident. Replace oldsite.com with app.newsite.io and the stored prefix becomes s:11:"app.newsite.io" — a length that no longer matches. unserialize() returns false, and any plugin that depends on that option (page builders, theme settings, WooCommerce shipping zones, ACF field groups) loses its state.
The two places this hurts most:
wp_options.option_value— every plugin and theme stores config here, much of it serialized.wp_postmeta.meta_valueandwp_usermeta.meta_value— page builder layouts, ACF custom fields, gallery selections, repeater fields.
A second hazard: JSON inside serialized strings. Gutenberg block markup uses JSON in HTML comments (<!-- wp:image {"id":42,"url":"https://oldsite.com/..."} -->). That JSON is stored as a single string inside post_content — not serialized, but the URL is still wrapped in escaped quotes. A regex that replaces https://oldsite.com with https://newsite.com mostly works, but if the new URL contains characters that need JSON escaping (a colon followed by a port, ampersands, backslashes), the JSON parser fails.
A third hazard: encoded URLs in query strings. Tracking links and OAuth redirect URIs store the destination URL percent-encoded inside another URL. oldsite.com becomes oldsite%2Ecom in those values, and a plain search-replace misses them.
wp search-replace handles serialized data by walking the data structure, replacing the inner strings, and recomputing the length prefix. It does not rewrite percent-encoded variants — those need a second pass.
How to verify
Before you run any replace, inventory what you’re about to touch:
# Count rows where the old domain appears, by table
wp db query "
SELECT 'wp_options' tbl, COUNT(*) c FROM wp_options WHERE option_value LIKE '%oldsite.com%'
UNION ALL SELECT 'wp_postmeta', COUNT(*) FROM wp_postmeta WHERE meta_value LIKE '%oldsite.com%'
UNION ALL SELECT 'wp_usermeta', COUNT(*) FROM wp_usermeta WHERE meta_value LIKE '%oldsite.com%'
UNION ALL SELECT 'wp_posts', COUNT(*) FROM wp_posts WHERE post_content LIKE '%oldsite.com%' OR guid LIKE '%oldsite.com%'
UNION ALL SELECT 'wp_comments', COUNT(*) FROM wp_comments WHERE comment_content LIKE '%oldsite.com%' OR comment_author_url LIKE '%oldsite.com%';
"
Find rows that contain serialized data with the old domain — these are the high-risk rows:
wp db query "
SELECT option_name, LENGTH(option_value) len
FROM wp_options
WHERE option_value LIKE 'a:%' AND option_value LIKE '%oldsite.com%'
ORDER BY len DESC
LIMIT 20;
"
Always run wp search-replace with --dry-run first and read the per-table summary:
wp search-replace 'https://oldsite.com' 'https://newsite.com' \
--dry-run \
--all-tables-with-prefix \
--report-changed-only \
--skip-columns=guid \
--precise
What the flags do:
--all-tables-with-prefix— covers custom tables a plugin may have registered (WooCommerce lookup tables, Yoast indexables, action_scheduler logs).--report-changed-only— only print tables with non-zero changes; the summary becomes auditable.--skip-columns=guid— the GUID inwp_postsis an opaque identifier, not a URL to be followed. WordPress documents this explicitly. Rewriting GUIDs invalidates feed subscribers’ read state.--precise— forces PHP-level serialization handling instead of the faster string-substitution path. Slower, but it’s the only mode that handles edge cases like serialized objects (O:8:"stdClass":...) correctly.
To spot percent-encoded variants:
wp db query "
SELECT 'options' t, COUNT(*) c FROM wp_options WHERE option_value LIKE '%oldsite%2Ecom%'
UNION ALL SELECT 'postmeta', COUNT(*) FROM wp_postmeta WHERE meta_value LIKE '%oldsite%2Ecom%';
"
To spot JSON-in-meta (Gutenberg blocks, ACF JSON fields):
wp db query "
SELECT ID, post_title FROM wp_posts
WHERE post_content LIKE '%<!-- wp:%oldsite.com%'
LIMIT 10;
"
The fix
A safe runbook. Run on a copy first, then on the cutover database.
1. Snapshot the database. Not optional. Search-replace is a destructive operation on every row it touches; you need a known-good restore point.
wp db export pre-search-replace-$(date +%Y%m%d-%H%M).sql.gz --add-drop-table
2. Run the primary pass with --precise.
wp search-replace 'https://oldsite.com' 'https://newsite.com' \
--all-tables-with-prefix \
--report-changed-only \
--skip-columns=guid \
--precise
If the site has both www. and bare-domain variants, run a second pass for the alternate:
wp search-replace '//www.oldsite.com' '//www.newsite.com' \
--all-tables-with-prefix --skip-columns=guid --precise
wp search-replace 'http://oldsite.com' 'https://newsite.com' \
--all-tables-with-prefix --skip-columns=guid --precise
3. Handle percent-encoded URLs. These appear in OAuth redirect URIs, tracking links, and some plugin configurations:
wp search-replace 'oldsite%2Ecom' 'newsite%2Ecom' \
--all-tables-with-prefix --skip-columns=guid --precise
4. Handle JSON-in-meta with --regex. For values where the URL appears inside a JSON-escaped string ("url":"https:\/\/oldsite.com\/..."), the forward slashes are escaped. WordPress’s serialized walker handles the outer envelope, but if a plugin stores raw JSON (not serialized), you need a regex pass that matches both escaped and unescaped variants:
wp search-replace 'oldsite\\\/com' 'newsite\\\/com' \
--all-tables-with-prefix --skip-columns=guid --precise --regex
Test regex on a single table first — a bad pattern can rewrite content you didn’t intend to touch:
wp search-replace 'oldsite\\\/com' 'newsite\\\/com' \
wp_postmeta --dry-run --regex
5. Replace file paths if the home directory changed. Migrating from /home/oldacct/public_html/ to /var/www/newsite/:
wp search-replace '/home/oldacct/public_html' '/var/www/newsite' \
--all-tables-with-prefix --skip-columns=guid --precise
This catches absolute paths stored in upload metadata, plugin caches, and import logs.
6. Verify nothing went sideways. Re-run the inventory queries from earlier — the row counts for %oldsite.com% should now be zero (or close to zero, if you intentionally skipped a table). Check that serialized data still unserializes:
wp db query "
SELECT option_name, option_value FROM wp_options
WHERE option_value LIKE 'a:%' AND option_value LIKE '%newsite.com%'
LIMIT 5;
" | php -r 'while($l = fgets(STDIN)) { $p = explode("\t",$l); if(count($p)<2) continue; $r = @unserialize($p[1]); echo $p[0], $r===false ? " UNSERIALIZE FAILED\n" : " ok\n"; }'
7. Flush caches. Object cache and page cache will be holding pre-replace values:
wp cache flush
wp transient delete --all
If WP Rocket, W3 Total Cache, or LiteSpeed Cache is installed, also call their CLI purge — wp cache flush only clears the built-in object cache layer.
8. Rotate any URLs that aren’t in the database. .htaccess, wp-config.php constants, cron entries that call wget against the old domain, monitoring checks. The database is the loudest source but not the only one.
The full runbook for a typical domain change takes about 15 minutes on a 5GB database; the inventory pass and the verify pass each take longer than the actual replace.
Stack Harbor runs domain changes, search-replace passes, and the surrounding cutover work for production WordPress under enterprise WordPress hosting and our migration practice. If your last search-replace left “the content was modified outside of Elementor” notices in your dashboard, we can clean it up.