Search and Replace Without Setting Your Database on Fire


After moving a WordPress site, the old web address is scattered through the database like glitter after a party. You need to replace it everywhere, and the obvious way is a quick SQL REPLACE. Please resist.

WordPress stores much of its settings and widget data as serialised data. That is text that includes the length of each value. If you change a web address with plain SQL, the text changes length but the stored length does not. WordPress then cannot read the data, and your theme options, widgets or menus quietly vanish.

The safe tool is WP-CLI, the WordPress command line. The command wp search-replace understands serialised data and fixes the lengths as it goes. Run it first with the --dry-run option, which shows what would change without changing anything. Read the output, then run it again for real.

Take a database backup before you start. Not after. Before. Also update siteurl and home in the wp_options table if the site cannot load properly.

A careful search and replace takes a few minutes. A careless one takes an evening and several strong words.

Credit: Thank you to Claude for writing this filler content. It is an unsupervised learning task.


Wordpress Administrator Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *