WordPress Search Replace Database URL Without Breaking Data
Change a WordPress site URL in the database without blanking widgets, menus or theme settings. Use the WP-CLI command that keeps serialized data intact.
Do not run a plain UPDATE ... SET column = REPLACE(column, 'oldurl', 'newurl') across a WordPress database. It will change the text correctly and destroy your settings at the same time, because WordPress stores arrays and objects as PHP-serialized strings, and that format records the byte length of every string it contains. A raw SQL replace rewrites the text but never touches the recorded length, and the value stops being readable.
Use a serialization-aware tool instead. On the command line that is wp search-replace. Back up the database first, run it as a dry run, then run it for real.
TL;DR: a plain SQL REPLACE on a WordPress database breaks any serialized value (widgets, theme mods, page builder data) because it changes the text without updating the byte-length number PHP’s serializer stores alongside it. The fix is wp search-replace, which decodes, replaces, and re-serializes with correct lengths. Back up first, dry-run it, then run it for real, and skip the guid column.
This usually happens mid-migration: the database import from staging has just finished, and swapping the old URL for the new one looks like a one-line fix. That single REPLACE statement is where the damage happens, and nothing tells you it happened.
What actually breaks
A PHP-serialized string looks like this:
s:18:"http://example.com"
The 18 is the byte length of the text that follows. http://example.com is exactly 18 characters.
Now imagine a stored theme setting:
a:1:{s:4:"logo";s:18:"http://example.com";}
Run a plain SQL replace of http:// to https:// and you might expect:
a:1:{s:4:"logo";s:19:"https://example.com";}
What you actually get is this:
a:1:{s:4:"logo";s:18:"https://example.com";}
The string is now 19 bytes but still claims to be 18. PHP reads the declared length, walks 18 bytes forward, does not find the closing quote and semicolon where it expects them, and gives up. The whole array fails to unserialize. PHP’s unserializer trusts the number, not the content.

get_option returned false for the serialized value, and the plain URL still read back correctly.Why it fails silently
This is the part that makes the bug so expensive. WordPress does not throw an error and stop. When it reads an option or a meta value it passes it through maybe_unserialize(), PHP returns false for the malformed data, and your code gets false, the same value get_option() returns for an option that doesn’t exist. The plugin or theme then falls back to its default.
So the symptom is not an error page. The symptom is:
- Widgets disappear from sidebars
- Customizer settings reset to theme defaults
- A page builder’s layout data comes back blank and the page renders empty
- Plugin settings pages look freshly installed
- Menus lose their assigned locations
Nothing in the admin says anything is wrong, and the debug log doesn’t either. PHP’s unserialize() does raise a warning for malformed input (an E_WARNING since PHP 8.3, a notice before that), but maybe_unserialize() calls it as @unserialize(), and the @ operator suppresses that diagnostic. Turning on WordPress debug mode won’t bring it back.
We checked this on 16 November 2026 (WordPress 7.1.4, PHP 8.3.36, WordPress Playground, WP_DEBUG and WP_DEBUG_LOG on, display off). We saved a serialized option and a serialized post meta value, ran the plain SQL replace on both, and read them back the normal way. get_option() and get_post_meta() returned false, an untouched copy of the option still read back as its array, and debug.log was never created. As a control, passing the same stored string to unserialize() without the @ wrote PHP Warning: unserialize(): Error at offset 40 of 44 bytes to wp-content/debug.log, so logging itself worked. The PHP manual adds one exception: an error handler installed with set_error_handler() is still called for suppressed errors, so a debugging plugin that sets one may report what debug.log doesn’t.
The damage is also not evenly spread. Simple scalar values, such as siteurl, home, plain post content and a URL sitting in its own column, survive a naive replace perfectly. Only serialized values break. That is why a bad replace often looks like it worked at first glance.
Find the values that broke
Because nothing is logged, you have to look for broken rows yourself. This read-only script lists options and post meta rows that look serialized but no longer unserialize:
<?php
// List options and post meta that look serialized but no longer unserialize.
// Run with: wp eval-file check-serialized.php
global $wpdb;
$tables = array(
$wpdb->options => array( 'option_name', 'option_value' ),
$wpdb->postmeta => array( 'meta_key', 'meta_value' ),
);
foreach ( $tables as $table => $cols ) {
list( $key, $value ) = $cols;
$rows = $wpdb->get_results(
"SELECT {$key} AS k, {$value} AS v FROM {$table}
WHERE {$value} LIKE 'a:%' OR {$value} LIKE 'O:%' OR {$value} LIKE 's:%'"
);
foreach ( $rows as $row ) {
$v = trim( $row->v );
if ( is_serialized( $v ) && false === @unserialize( $v ) ) {
echo $table, ': ', $row->k, "\n";
}
}
}
Save it as check-serialized.php outside the web root and run wp eval-file check-serialized.php; wp eval-file loads WordPress before it runs the file. In the 16 November test above, the script printed exactly the two rows we had broken, wp_options: fht_ser and wp_postmeta: fht_meta, and skipped the untouched copy and the plain-text option. We loaded the file inside a WordPress request there, not through WP-CLI itself. On a large site the query reads every matching row into memory, so run it on a copy or at a quiet time. The same pattern works for $wpdb->usermeta and $wpdb->termmeta if you add them to the list.
Check wp-config.php before touching anything
If wp-config.php defines WP_HOME or WP_SITEURL, those constants win over whatever is in the database. Changing wp_options will appear to do nothing at all. Look for lines like these before you conclude the database is the problem:
define( 'WP_HOME', 'http://example.com' );
define( 'WP_SITEURL', 'http://example.com' );
Either update them to the new URL or remove them and let the database drive. Our guide to wp-config.php shows where the file is and what these two lines override.
Back up first, and mean it
A serialization break is not always easy to reverse. Once the length prefix and the content disagree, the original value is not recoverable from the broken value: you cannot know what the correct length was supposed to be without knowing the original string. A backup is the only real undo.
wp db export backup-before-replace.sql
Or with mysqldump if WP-CLI is not available:
mysqldump -u USER -p DBNAME > backup-before-replace.sql
Keep it off the server if you can. A backup sitting in the web root is both a restore point and a security problem.
The safe command
WP-CLI’s search-replace unserializes each value, walks the decoded structure, replaces inside it, and re-serializes with correct lengths. Its documentation says it “intelligently handles PHP serialized data, and does not change primary key values.” Dry run first:
wp search-replace 'http://example.com' 'https://example.com' --dry-run --all-tables-with-prefix --skip-columns=guid
Read the report. It tells you how many replacements will happen per table and column. If the table list looks wrong, or a table you do not recognize has thousands of hits, stop and look before running it for real.
Then run it:
wp search-replace 'http://example.com' 'https://example.com' --all-tables-with-prefix --skip-columns=guid --report-changed-only
Which tables the command touches depends on the flags. All quotes below are from the wp search-replace documentation:
| Flag | Tables searched |
|---|---|
| None | ”tables registered to the $wpdb object”. On multisite, “just the tables for the current site unless —network is specified.” |
--network | ”all the tables registered to $wpdb in a multisite install” |
--all-tables-with-prefix | ”any tables that match the table prefix even if not registered on $wpdb”, which is where plugin tables usually live |
--all-tables | ”ALL tables in the database, regardless of the prefix, and even if not registered on $wpdb” |
--all-tables-with-prefix is the right default for most sites. --all-tables is only safe when the database holds nothing but this one site, because it also rewrites the tables of any other install that shares the database under a different prefix.
More notes on the flags:
--skip-columns=guidmatters. Theguidis a permanent identifier for feed readers, not a live URL. Rewriting it can make feed subscribers see your entire archive as new posts. The documentation’s own example uses the same flag.- The documentation also says “Tables without a primary key are skipped.” If the dry-run report leaves out a plugin table you expected, that is a likely reason.
--preciseforces “the use of PHP (instead of SQL) for all columns.” It is slower; use it when a dry run seems to miss values you can see in the table.- Search without the trailing slash and without the protocol variants you do not want to touch. Replacing bare
example.comwill also hit email addresses and any mention in post text.
If you cannot use the command line, serialization-aware migration plugins do the same decoding work through the admin screen. The behavior you are looking for is explicitly described as handling serialized data; if a tool does not say so, assume it does not.
If this replace is part of a move from http to https, our guide to mixed content after moving to HTTPS covers the rest of that switch, including the .htaccess redirect.
Which tables and columns are involved
| Table | Column | Serialized risk |
|---|---|---|
| wp_options | option_value | High: widgets, theme mods, transients, most plugin settings |
| wp_postmeta | meta_value | High: page builder layouts, custom field arrays |
| wp_usermeta | meta_value | High: capabilities, screen and dashboard preferences |
| wp_termmeta | meta_value | Medium, depends on plugins |
| wp_posts | post_content, guid | Low for content, but shortcode attributes and block markup carry URLs |
| wp_comments | comment_content, comment_author_url | Low |
| wp_links | link_url, link_image | Low, and often unused |
The names above use the default wp_ prefix. Yours is whatever $table_prefix says in wp-config.php, and that is the prefix --all-tables-with-prefix matches. (The wp-config.php generator sets it for a new install, with a warning if it contains characters WordPress won’t accept.)
On multisite, add wp_blogs, wp_site, wp_sitemeta, and the per-site wp_2_options, wp_3_options and so on, and add --network or --all-tables-with-prefix so the other sites’ tables are included. Multisite domain changes have extra rules beyond a search and replace, so treat that as a separate job.
The cases search-replace does not fully solve
JSON stored inside the database. Some page builders store layout data as JSON with escaped forward slashes, so your URL appears as https:\/\/example.com rather than https://example.com. A search for the normal form will miss it. You may need a second pass on the escaped string. Test on a copy.
We checked the string matching on 6 November 2026 with a short Node.js v25.9.0 script; it tests the text search only, not WP-CLI or WordPress. Encoding http://example.com the way PHP’s json_encode() does by default (escaping slashes unless JSON_UNESCAPED_SLASHES is passed) produces http:\/\/example.com. Searching that stored value for the plain, unescaped URL found no match, so any replace that looks for the plain form passes over the row without an error. Searching again for the escaped form (\/ in place of /) found and replaced it. If a dry run shows zero hits in a column you know holds the old domain, check for this before assuming the data is not there.
Base64-encoded values. If a plugin encodes settings before storing them, no text-based replace can see the URL at all. Those settings usually need to be re-entered through the plugin’s own screen.
Hardcoded URLs in files. Anything written into functions.php, a child theme template, or a hardcoded asset path is not in the database and will not be touched. Grep the theme directory separately.
Cached and generated output. Object cache, page cache, and CDN copies keep serving the old URLs after a correct replace. Flush them, then check again before assuming the replace failed.
If you already ran the bad query
Restore the backup from the step above. That is the answer, and it is faster and more complete than any repair.
If there is no backup, the recoverable path is narrow. Values that were never serialized are fine. Run the script above to list the broken ones, then reset them by re-saving the relevant settings screen: re-save a widget area, re-select a menu location, or re-enter a plugin’s settings, and WordPress writes a fresh, correctly serialized value over the broken one. It is tedious, but it works, and it is better than leaving corrupted rows in place where they will confuse the next person who looks at the site.
To see the queries before running anything, build them with the WordPress search and replace SQL tool. It shows which columns are safe to hit with plain SQL and which ones need serialization-aware handling, so you can see the split before you commit to a command.
Sources
- WP-CLI handbook: wp search-replace, wp eval-file
- WordPress Advanced Administration: Moving WordPress
- WordPress Developer Resources: maybe_unserialize(), is_serialized()
- WordPress 7.1.4 source code: functions.php (maybe_unserialize)
- PHP manual: serialize(), unserialize(), Error control operator (@), json_encode()
William Lopez is the founder of Odie Themes. He builds the free WordPress tools and themes on this site and has been on WordPress.org since December 2019.
FAQ
Questions
Why does a plain SQL REPLACE break a WordPress site?
Because WordPress stores arrays and objects as PHP-serialized text, and that format records the byte length of every string inside it. A raw REPLACE changes the text but leaves the old length number in place. PHP then refuses to unserialize the value, WordPress falls back to a default, and settings appear blank rather than wrong.
What is the safest way to change a WordPress site URL in the database?
Take a full database backup, then run the WP-CLI command wp search-replace with a dry run first. WP-CLI unserializes each value, replaces inside the decoded structure, and re-serializes with corrected lengths. Serialization-aware migration plugins do the same job through the admin screen if command line access is not available.
Which tables hold the old URL?
Mainly wp_options, wp_posts, wp_postmeta, wp_usermeta, wp_termmeta and wp_comments. The serialized danger lives in wp_options and in any meta_value column, since widgets, theme mods, transients and page builder data are stored as arrays. On multisite, wp_blogs, wp_site and wp_sitemeta also carry the domain.
Does changing the database fix the URL if wp-config.php defines it?
No. If wp-config.php defines WP_HOME or WP_SITEURL, those constants override whatever siteurl and home hold in wp_options. Editing the database changes nothing visible until the constants are updated or removed. Check wp-config.php first, since that single line explains many failed migrations.
Should I replace the guid column too?
No. The guid is a permanent identifier for feed readers, not a working URL, and rewriting it can cause feed subscribers to see every old post again as new. Pass skip-columns with guid when running WP-CLI. The only common exception is a site that never had public feeds or subscribers.
How do I know whether a value is broken after a bad replace?
Broken serialized values look normal in the database but come back empty in WordPress. Widgets vanish, theme customizer settings reset, and plugin options revert to defaults with no error message. debug.log stays empty too, because WordPress unserializes with PHP's @ operator. Compare the declared string length against the real byte count, or run a short script that lists values which look serialized but no longer unserialize.