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
  • Wayne Williams 12 / Mar / 2012 at 11:43 am

    Hi, I just used your plugin to change my domain name. It worked great, but it did not change any of the links in text widgets (I had selected search and replace “all”). Any suggestions?

    • David Coveney
      David Coveney 12 / Mar / 2012 at 11:51 am

      You need to form your search string correctly.

      It’s a search and replace, and it will only find absolute matches – if your string doesn’t match it won’t be able to do the replace.

  • [...] 2012 Update: This script here makes all these steps so much easier: http://interconnectit.com/124/search-and-replace-for-wordpress-databases/ Posted in cPanel, WordPress 3 [...]

  • Clifford 9 / Mar / 2012 at 1:43 pm

    Thank you for this script. I found it at the bottom of the Moving WordPress codex page. I have successfully moved my blog from example.com/blog/ to example.com/ (THANK YOU) and noticed that the old post URLs get a nice 301 redirect to the non-/blog/ URL. But I looked at my .htaccess file and there’s no rule in there to do that. And I looked in _postmeta table where meta_key = _wp_old_slug and realized that only applies when the slug changes, which doesn’t apply to what I did.

    And I searched through the db export to see if I could find something and couldn’t so I’m asking you…
    1) How does WP know to redirect from example.com/blog/ to example.com/ ? Is it simply the siteurl field that changed and your script that edited the db and that I put the files in the non-/blog/ location?
    2) In searching through the db, I noticed there were still a lot of references to, for example: example.com/blog/content.jpg. And when I typed that full URL in, it retured 404. In other words, that’s a confirmation that there’s not a “hard” redirect on /blog/ to /.
    3) Other than in an .htaccess file, is there a table/field in WP that I could enter a list of redirects? For example, I used to have example.com/blog/abc/def and now that same page/post has example.com/xyz. Your script takes out the /blog but I need to change /abc/def to /xyz. If it were just the slug and not the path before the slug, I would just enter these changes in the _wp_old_slug field, but I need to be able to change the path before the slug. And, ideally, I’m asking how to do this without .htaccess – even if I have to hand-enter each one instead of having a rule. There aren’t that many to do and there’s a variety of scenarios so one-by-one is the only way to do it. I hope this question makes sense.

    THANK YOU SO MUCH! :)

    • David Coveney
      David Coveney 9 / Mar / 2012 at 5:30 pm

      WP will usually do its best with malformed URLs to find the nearest match – that’s why you’re getting hits rather than 404s

      With respect to the db references being old, that’s because you haven’t used the right search/replace terms – eg, http://example.com to http://example.com/blog should be done as example.com to example.com/blog or vice versa. You have to be very careful about trailing slashes and the likes – make the search term as terse as possible, or be prepared to do a few search and replaces with different variations.

  • Tosin 6 / Mar / 2012 at 6:58 pm

    Fantastic script. Many thanks.

  • David Herse 5 / Mar / 2012 at 1:45 am

    Thanks! Simple. Brilliant.

  • fastidious 4 / Mar / 2012 at 6:29 am

    Finally I got my local mamp website live. Thanks to the script and the tutorial as well, along with about 10 other guides I’ve gleamed information from. THANKS

  • fastidious 4 / Mar / 2012 at 1:38 am

    Should I be running this script on the live server after I’ve imported the database and linked it ? Or should I be running the script on localhost before exporting the database file? Or should I be able to do it either way?

  • Nick Julia 3 / Mar / 2012 at 10:18 pm

    Great tool. Have used it on multiple wordpress installations and you guys have truly saved us alot of time. Thanks alot.

  • Teemu Kinnunen 3 / Mar / 2012 at 9:46 am

    Thanks you SO MUCH! You saved my ass. Where’s the donate button ?

    • David Coveney
      David Coveney 3 / Mar / 2012 at 11:27 pm

      No facility to accept donations as yet, but maybe one day. Feel free to donate to the charity of your choice instead!

  • chris 2 / Mar / 2012 at 5:28 pm

    Thank you so much, this just saved my ass! I originally tried a full search and replace across the entire DB and it messed everything up, but this worked perfectly :)

  • Angie 2 / Mar / 2012 at 6:29 am

    Love it! Absolute brilliance.

  • Scott 2 / Mar / 2012 at 4:18 am

    Thanks for creating this wonderful script. Worked perfect the first try on my WordPress installation and saved me a ton of time!

  • [...] WordPress Serialized PHP Search Replace Tool | Interconnect IT [...]

  • Hagai Hadad 29 / Feb / 2012 at 9:06 pm

    rookie question – a little new to php programming.
    Where exactly do I put the strings I need to find and replace with and any other parameter i need to modify?
    I took a look at the script and it said “To use, simply configure the settings below and off you go”. where exactly is below in the code?

    I know its a rookie question, but please help me.
    Sincerely,
    Hagai Hadad

    • Robert O'Rourke
      Robert O'Rourke 1 / Mar / 2012 at 10:07 am

      You don’t need to look at the code to use the script. Once you have placed the script on your server point your web browser to the file itself eg: http://example.com/searchreplacedb.php and you will see an HTML page with instructions. Follow those and you will be prompted with fields to enter the text to text to find and what to replace it with.

      Hope that helps…

  • [...] Disclaimer: Use at your own risk. Theme & plugin authors may have chosen to store options in other ways than in it’s own folder or in the database, so that the settings are lost in the move. Never delete anything from the old site until you are absolutely sure the new site works. Menu and widget positions and content may be lost because of them being stored as serialized data. (Consider using this.) [...]

  • Wikaina 26 / Feb / 2012 at 9:07 pm

    Works as advertised. Thank you!

  • Rob 25 / Feb / 2012 at 10:00 pm

    Thanks a million! I think this corrected the last of my 404s. I migated from Blogger self hosted to WP self hosted and this was just the trick for some images that didn’t import.

  • keesiemeijer 21 / Feb / 2012 at 2:28 pm

    Thank You! I will add this to the WordPress codex (if it’s not already in there).

    • David Coveney
      David Coveney 21 / Feb / 2012 at 5:26 pm

      Thanks Kees! I’m assuming Kees is indeed your first name…

      Really appreciated.

      • keesiemeijer 22 / Feb / 2012 at 11:03 am

        Yes it is. I added it here : http://codex.wordpress.org/Moving_WordPress#Moving_WordPress_Within_Your_Site
        Let’s hope they will let it in there and will look for more places to add it.

        When you skip the guid column it also skips the guid for attachments (WordPress). Shouldn’t the attachement guids be replaced when migrating live sites? Maybe a new option for this?

        • David Coveney
          David Coveney 22 / Feb / 2012 at 11:14 am

          Thanks for adding it!

          Ah, you make a good point about the attachment guids – skipping those could, in some cases, cause a problem. It’s very rare for us to move a site between URLs and all the aggregation work we do is curated so it doesn’t cause issues there BUT for some it’s important, so we’ll look to adding it to the project on GIT at https://github.com/interconnectit/Search-Replace-DB – even better if somebody wants to contribute the change – it’ll be a moderate but not extreme amount of work.

  • Jonas Goderis 17 / Feb / 2012 at 8:35 am

    Thank you for this script, fixed all my problems!!!

  • Stephen 15 / Feb / 2012 at 8:02 am

    You just saved my life.

  • Lucius 14 / Feb / 2012 at 6:30 pm

    Hey man. thanks for your work on this script..
    wordpress needs to add this link to change server FAQ’s

    very very useful

    thanks again

  • Asif Ali Siddiqui 9 / Feb / 2012 at 7:53 pm

    this is good work!!

  • Marlon 31 / Jan / 2012 at 2:14 pm

    Thanks for hard work and share it!

  • [...] data which is not easily fixed if you try to do any search and replace. Now there is a way which does that for you but only when you follow those steps to the T. Well, we reckon and hope that the humble [...]

1 3 4 5 6 7 9