1

The frustration of serialized data in WordPress has been around for years... trying to search and replace strings in your database using tools like WP-CLI, MySQL queries, or even plugins like Better Search Replace, are not always* effective for serialized data that is used for things like widgets, themes, page builder plugins, and more.

I thought this problem deserved a WPSE question.

What is the best way to deal with searching and replace large amounts of instances in the database when traditional search and replace tools are not properly updating the data? This is particular relevant to replacing URLs.

Edit: added the word always

Edit #2: I'm not trying to say WP-CLI doesn't work, I'm not an expert on WP-CLI nor serialized data, since I tend to use custom themes and lots of hardcoded templates. From the cases I have read about, however, WP-CLI has failed at times with search/replacing serialized data.

10
  • 4
    The WP CLI command has worked for serialized data in my experience.
    – WebElaine
    Aug 18, 2022 at 21:28
  • 1
    @JesseNickles This is the developer link for wp-cli. It states that it will search serialized data and provides example searches. developer.wordpress.org/cli/commands/search-replace
    – rudtek
    Aug 18, 2022 at 22:00
  • 2
    @JesseNickles what have you specifically tried (e.g. commands you used) and what's the outcome? Can we see a sample of the serialized string and the string you tried to replace, and the replacement string, and what tables might contain the string?
    – Sally CJ
    Aug 19, 2022 at 2:23
  • 1
    Well I admit I wanted to know details like the specific commands you used, but I wasn't referring to just WP-CLI, and I'm sorry that I didn't tell you I've checked those links. Nonetheless, I don't have an answer for you, so good luck in finding that best way, and you might better off ask on a forum and not a QA site like this.
    – Sally CJ
    Aug 19, 2022 at 10:42
  • 1
    While definitely useful and a topic worth exploring, I do think this falls outside the site scope and format in that it may solicit product recommendations and can only really be answered with opinions of relatively equivalent validity. All tools tend to break at some point or another, and may or may not cater to certain use-cases, but without a specific use-case to address there is no definitive solution. All of that said, I used to use Inteconnect's tool religiously prior to switching to WP CLI - I haven't experienced issues for my use-cases with either tool.
    – bosco
    Aug 19, 2022 at 18:12

1 Answer 1

2

It seems WP-CLI is indeed one of the easiest ways to search/replace serialized strings, e.g. for replacing any unwanted instances of staging links:

wp search-replace https://staging.example.com https://example.com --all-tables

The above command will search the entire WordPress database for the first string, and replace all instances with the second string provided. If you want to test it first, try this:

wp search-replace https://staging.example.com https://example.com --all-tables --dry-run

You can also target certain sites in a Multisite network using this approach:

wp search-replace --network --url=example.com https://staging.example.com https://example.com --precise --all-tables

On one particular website I was trying to optimize, Better Search Replace plugin had failed to replace all the serialized strings caused by the page builder plugin (WP Bakery). I had tried to replace the full URL strings exactly like above, and also the domain names without the https:// prefix in my strings... but it failed to cleanup the broken links around the website.

I tried again with WP-CLI and it also failed to cleanup the website until I dropped the https:// prefix from my strings, and then it worked perfectly. I discovered wp_options and wp_postmeta tables were also affected by this second attempt, when initially only wp_posts table was affected.

This probably depends on how certain plugins and themes store and retrieve their data... it doesn't make sense to me since editing a page in WP Bakery displays the full absolute URL in the editor, but for some reason the other serialized data in the other (non-wp_posts) tables had to be cleaned up also, to fix all broken links.

P.S. I'm not a fan of answering WPSE questions just to say "use WP-CLI" without providing actual code snippets, so I will update this answer later if I find other methods.

1
  • Update: I can confirm even this advanced method doesn't work in some cases, for example sites built with Elementor page builder. Thankfully they have their own search/replace tool now, although I find it unacceptable that standard search/replace methods don't work for certain vendors' products. Ref: github.com/elementor/elementor/issues/10766 Aug 1, 2023 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.