PHP Serialization Fix for WordPress Migrations (& other applications like Expression Engine)

A different kind of migration - public domain from Wikipedia Commons

A dif­fer­ent kind of migra­tion — pub­lic domain from Wiki­pe­dia Commons

When you move a Word­Press blog from one folder to another, or from one site to another, you nor­mally use the export/import functionality.

This is fine for nor­mal blogs, but say you’ve developed a new web­site and set it up on your local machine — the URL for the site may be some­thing like http://localhost/devsite and the live URL will be some­thing like http://www.davesgonemental.com — you won’t want to set up all the theme options, site options, plu­gin options and so on all over again.

Instead, a the­or­et­ic­ally simple approach is to do a data­base dump, a search and replace for all ref­er­ences to server paths and URLs, and then reim­port that data in the new location.

Should work, but it often falls apart.

What hap­pens is that in Word­Press, its themes and its plu­gins, a lot of data is stored using a method known as seri­al­iz­a­tion.  Now, in my opin­ion this breaks all known good prac­tice around data — it’s lan­guage spe­cific, it’s not rela­tional even though it often could be, and it’s hard to edit by hand.

One par­tic­u­lar prob­lem is that if you change the length of the data in a seri­al­ised string you have to change the length declared in the gen­er­ated string.

That’s very pain­ful when you have hun­dreds of the fields.

So, because I’d found this pain­ful I decided to knock together a quick applic­a­tion to at least reduce the amount of edit­ing I had to do.  You just do your search and replace, for­get about the seri­al­ized string lengths, upload your data to the new data­base, and run this script.

Warn­ing: I haven’t got it to work for wid­gets and cForms II yet, but the lat­ter has some export func­tion­al­ity any­way, which takes that par­tic­u­lar pain away if you plan ahead.  In the mean­time, feel free to play with the attached file.  You use it at your own risk, of course.

To use it, down­load the file linked in this post, extract it, open the file, edit the con­nec­tion set­tings, tell it the table you want to scan through, the column, and the unique key field.  If you some­how man­age to have more than one unique key to deal with (you shouldn’t, but then it sur­prises me what people man­age to code up), then you’ll have to modify the code accord­ingly.  Once done, make sure you have a backup of that table, and execute the php — either at the com­mand line or through the browser. License is WTFPL, and if you’d like to improve the code, please do and I’ll host the new version.

Serialization-fixer.zip download.

down­load file

Serialization-fixer.zip down­load

BIG WARNING: I take no respons­ib­il­ity for what this code does to your data. Use it at your own risk. Test it. Be care­ful. OK? Here in the North we might describe the code as being as “Rough as a badger’s arse.” Never felt a badger’s arse, but I’ll take their word for it.

Trackbacks Comments
  • You’re a life saver, Dave. I used this script to fix seri­al­iz­a­tion issues in Expres­sion Engine… many thanks.

    • Thanks Sean — glad to hear it was use­ful for you. When you put these things out you never know whether it’ll help people or not — gives a nice warm fuzzy feel­ing to hear that it does :-)

  • You should label it not only to fix Word­press, but as a gen­eral fix for cor­rup­ted seri­al­ized data­base records — being able to define the db, table, index and column makes it flexible.

    Per­haps the next step would be allow­ing a user to define an array of table columns instead of one at a time… and per­haps echo­ing a status at the end :)

    Maybe even a clever name…

    In any case, it’s a big help as is.

    • Aye — good point. I wrote it to solve a very spe­cific prob­lem, using snip­pets I’d found else­where but wrapped up in a single script.

      I’m plan­ning to write a script that lets you do a search and replace on a table or even entire data­base, deseri­al­iz­ing and reseri­al­iz­ing as neces­sary. Ulti­mately, there needs to be a simple way for people to eas­ily and quickly change val­ues in seri­al­ized data. Altern­at­ively, PHP developers could just learn some good data­base prac­tices, but I’m not hold­ing my breath ;-)

  • Ulti­mately, there needs to be a simple way for people to eas­ily and quickly change val­ues in seri­al­ized data”

    Agreed!!

  • Inter­est­ing! I just wrote a script to copy a word­press install to a new domain, and am run­ning into the exact same prob­lem. The script works except for widgets.

    Any idea exactly what part of wid­gets is being broken? Maybe I can fig­ure it out for both of us.

    Thanks!

    • Bur­ton — I think the prob­lem is down to nes­ted arrays — it’s just too hard a prob­lem for my simple regex fix to solve.

      What’s needed instead is a script to read a data­base, pick up what’s seri­al­ized php, unseri­al­ize it, carry out the search and replace, and then re-serialize the data and put it back into the data­base. But that’s a con­sid­er­ably big­ger job.

      My script is, essen­tially, a stick­ing plaster to fix most of the prob­lems. You could do the wid­gets by hand — at least then there’s not too much to do. Everything else seems to be OK, though I’ve noticed some plu­gins like cForm­sII do this too, so that’s another manual job.

Leave a Comment

Threaded commenting powered by Spectacu.la code.