Loading...

WordPress (and others) Search and Replace Tool

Version two brought a smarter UI and better workflow

When you’re migrating WordPress (or any other platform using serialized PHP strings in the database) between domains, you must use a safe search and replace method that preserves the integrity of the serialized string lengths. A simple of a dump file for http://localhost to, for example, http://thenewdomain.com is problematic because the length of the string changes but the indexes for the serialized strings does not. Consequently settings are lost and widgets disappear. Not good.

This script can now also handle multiply nested serializations, which can happen in transient values in WP at times, and it can also handle multi-byte Unicode changes safely. This is important now that internationalised domain names are allowed.

It’s Not Only for WordPress

It’s worth mentioning that the code will work for any platform that stores PHP serialized arrays in a MySQL database. You can easily use this script on Drupal, Joomla and many other systems where you need to change items across a database without messing up your stored arrays.

Download Search Replace DB v 2.1.0

Installation & Use

To use the script, you should install it in the root folder of your WordPress  install (if you wish it to automatically pick up your wp-config) or anywhere else you fancy, but you won’t get the automatic config.  You should also, to protect yourself from automated scanners looking for this script, rename it first.  eg, you could name it as rrrrreplace.php – you’d then visit a url like http://example.com/rrrrreplace.php and follow the on-screen instructions from there.

What you absolutely, 100% certainly MUST do is to delete the script once you’ve finished.  If somebody chances on it they can do anything to your database – and that wouldn’t be nice, would it?

If you’re using the script with systems such as Drupal or Joomla, everything should work, but we haven’t added any code to pick up on your config.  You may also have issues with Drupal blobs, in which case you should head on over to GIT (see link below) to grab the development copy which has handling for this included – feedback would be helpful, thank you.

Contribute!

A lot of developers now use this plugin. If you feel you can add something, there is now an official Github repository for this script at https://github.com/interconnectit/Search-Replace-DB

Version 2.1.0 (released Dec 2011) Improvements

The latest version 2.1.0 introduced a number of key improvements:

  1. Added charset support, helping in all sorts of places (thanks Sergey Biryukov!)
  2. Added option to skip guid column for migrating already live sites
  3. Removed use of deprecated functions.
  4. Other bug & comment fixes

Changes being prepared for Version 2.2.0

  1. Support for BLOBS/Objects to give Drupal compatibility
  2. Minor tweaks and corrections

github Repo – DRUPAL devs please test master branch!

To see how you can use this tool to aid migrations, check out our article on WordPress migrations or visit the WP Tuts+ article that mentions this script.

If you are in any doubt whatsoever about how to use this standalone script, then please consider getting an expert in.  It’s a really powerful bit of code that if used badly can damage a WP install beyond repair.  If you want help, get in somebody like us, for example, or any of the other great guys listed over at CodePoet.

IMPORTANT: This code is supplied with no warranty or support implied. You use it entirely at your own risk. Currently it is supplied under the WTFPL but this will change to the GPL eventually. And when you’ve finished using the script, PLEASE delete it as it can pose a serious security risk to your site.

Download Search Replace DB v 2.1.0

Changelog:

// Version 2.1.0:
// – Changed to version 2.1.0
// * Following change by Sergey Biryukov – merged in and tested by Dave Coveney
// – Added Charset Support (tested with UTF-8, not tested on other charsets)
// * Following changes implemented by James Whitehead with thanks to all the commenters and feedback given!
// – Removed PHP warnings if you go to step 3+ without DB details.
// – Added options to skip changing the guid column. If there are other
// columns that need excluding you can add them to the $exclude_cols global
// array. May choose to add another option to the table select page to let
// you add to this array from the front end.
// – Minor tweak to label styling.
// – Added comments to each of the functions.
// – Removed a dead param from icit_srdb_replacer

// Version 2.0.0 – returned to using unserialize function to check if string is serialized or not
// – marked is_serialized_string function as deprecated
// – changed form order to improve usability and make use on multisites a bit less scary
// – changed to version 2, as really should have done when the UI was introduced
// – added a recursive array walker to deal with serialized strings being stored in serialized strings. Yes, really.
// – changes by James R Whitehead (kudos for recursive walker) and David Coveney 2011-08-26

// Version 1.0.2 – typos corrected, button text tweak – David Coveney / Robert O’Rourke
// Version 1.0.1 – styling and form added by James R Whitehead
// Version 1.0.0 – original version by David Coveney

To Be Done

Ensure UTF8 encoding is enforced (see comments). Added in v2.1.0
Self deletion or security system to prevent accidental security risks.
Release CLI version for use on non-WP sites, or for other purposes (already supports use on any MySQL DB.)
Change to GPL V3.
Eliminate warnings and remove deprecated function calls. Added in v2.1.0
Add facility to subscribe to interconnect/it Newsletter.

Download Search Replace DB v 2.1.0

Donations

We’ve been asked a lot in the comments box below about accepting donations. But you can’t believe what a headache that is from an accounting and tax perspective.

Consequently all we can say is that if you wish to you can buy a personal gift for the key developers from one of the wishlists below – especially given that it’s a spare time project. If others who have contributed wish to provide us their wishlist links then we’d be more than happy to add them.

David Coveney is the project lead.
James Whitehead added the nice UI.
Robert O’Rourke gave us the branding and has made tweaks.

Comments
  • Ian 14 / May / 2012 at 3:54 am

    Hi, seems like a good script except for one minor problem that messed a whole site up. My dev server directory was wordpress2 and using this script it dropped the 2 off the end in 540 places. I was able to fix siteurl and home but went ahead and started back at square one with a new database rather than try to manually fix 540 instances where it dropped the 2 in the url.

    Other than that, it’s pretty awesome. Can you let me know if you find out why it dropped the number? Thanks

    • Robert O'Rourke
      Robert O'Rourke 15 / May / 2012 at 11:16 am

      Hi Ian,

      Can you tell us what the search/replace parameters you used were exactly? Would help if we can test it with the same set up.

1 7 8 9