WP-CLI search-replace --all-tables for Serialized PHP Arrays
Avoid broken Elementor layouts, lost widget settings, and corrupted theme options caused by raw SQL sed replacements on serialized strings.
--all-tables --precise · Preserves s:N:"..." byte counts · Zero broken widgets
Why Raw SQL REPLACE() Corrupts WordPress Options
WordPress stores navigation menus, WooCommerce shipping zones, and Elementor/ACF schemas as PHP serialized arrays containing exact string byte lengths (e.g., s:24:"https://live-site.test"). Changing domain length via raw SQL invalidates the byte count and causes PHP unserialize() to return false.
Safe WP-CLI Serialization Rewrite Flags
Always invoke wp search-replace "$LIVE_URL" "$STG_URL" --all-tables --precise --recurse-objects. Including --all-tables ensures custom plugin tables outside $wpdb->prefix are rewritten, and --dry-run validates row counts before mutating staging.
Measured Thresholds & Configuration
| Rewrite Method | Serialized Length Check | Custom Plugin Tables | Production Safety |
|---|---|---|---|
| SQL sed / UPDATE REPLACE() | Breaks on domain length delta | Manual per table | Unsafe (drops widgets/menus) |
| wp search-replace --precise | Recalculates PHP byte offsets | Covered by --all-tables | Safe & deterministic |