0

I am not the professional WP developer, but I have a task to create the plugin in order to search and replace some strings in WPDB.

I already done the part with initializing plugin, the one that I can not find is how to perform a search-replace throughout the whole WP DB.

The client's WP site has a WooCommerce on it and is actually a multisite setup, so I need to cycle through every table and search for occurrences and replace them with custom string.

Also, there is WP CLI installed, but I'm not sure it'll work on a remote server, since all WP there is running inside some containers (not really familiar with remote config).

So, the question is: is it possible to make a search-replace query on WP programmatically without any other plugins (I already know about "Better search replace" and other ones available, but it's not the option).

Kind regards,

4
  • 2
    this is very much a "your mileage may vary" thing. While things like in bosco's answer might work 99.9% of the time, you don't want to be the 0.1%. I would not do such a S&R in an automated way on any mission critical site, just the chance of using the wrong terms is a good enough reason to make sure you have a good backup before you start and to inspect the results and test the functionality of the site after you do it Commented Jun 27, 2021 at 17:08
  • 1
    ^ this! If at all possible, I would probably try to implement the feature using WordPress's PHP or REST APIs... if you could tell us more about the replacements you wish to make, we might be able to provide a more useful solution.
    – bosco
    Commented Jun 27, 2021 at 17:11
  • Thank you for all the replies! I am already examining the code of BSR plugin. The S&R functionality is designed to create the anonymizer plugin, that will anonymize all the customers private information (such as email, name, last name, address, etc) in order to comply with GDPR. And yes, there is a backup, but of course it'd be better if the backup restore function won't be used
    – Max Krizh
    Commented Jun 27, 2021 at 17:16
  • 1
    Neato! I saw a similar feature in the WPMerge.io plugin recently, where when populating a staging site from a production site you are given the option to anonymize PII. I never tried it, but it is a compelling feature.
    – bosco
    Commented Jun 28, 2021 at 20:41

1 Answer 1

1

Is it possible to make a search-replace query on WP programmatically without any other plugins (I already know about "Better search replace" and other ones available, but it's not the option).

Succinctly, yes - the existence of "Better Search and Replace" is a testament to the possibility.

A search-and-replace by itself is simple - you can make the necessary SQL queries through the WPDB interface. The complexity in writing such a thing from scratch lies in whether or not that operation needs to affect fields which WordPress serializes, as performing a replacement in serialized data without unserializing and reserializing the string or otherwise updating the serialization will invalidate the data.

If using a pre-made plugin is not an option, studying such a plugin's code to see how they accomplish that feat would be a good move.

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.