When you move a WordPress blog from one folder to another, or from one site to another, you normally use the export/import functionality.
This is fine for normal blogs, but say you’ve developed a new website and set it up on your local machine – the URL for the site may be something like http://localhost/devsite and the live URL will be something like https://davidcoveney.com – you won’t want to set up all the theme options, site options, plugin options and so on all over again.
Instead, a theoretically simple approach is to do a database dump, a search and replace for all references to server paths and URLs, and then reimport that data in the new location.
Should work, but it often falls apart.
What happens is that in WordPress, its themes and its plugins, a lot of data is stored using a method known as serialization. Now, in my opinion this breaks all known good practice around data – it’s language specific, it’s not relational even though it often could be, and it’s hard to edit by hand.
One particular problem is that if you change the length of the data in a serialised string you have to change the length declared in the generated string.
That’s very painful when you have hundreds of the fields.
So, because I’d found this painful I decided to knock together a quick application to at least reduce the amount of editing I had to do. You just do your search and replace, forget about the serialized string lengths, upload your data to the new database, and run this script.
Warning: I haven’t got it to work for widgets and cForms II yet, but the latter has some export functionality anyway, which takes that particular pain away if you plan ahead. In the meantime, feel free to play with the attached file. You use it at your own risk, of course.
To use it, download the file linked in this post, extract it, open the file, edit the connection settings, tell it the table you want to scan through, the column, and the unique key field. If you somehow manage to have more than one unique key to deal with (you shouldn’t, but then it surprises me what people manage to code up), then you’ll have to modify the code accordingly. Once done, make sure you have a backup of that table, and execute the php – either at the command 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.
BIG WARNING: I take no responsibility for what this code does to your data. Use it at your own risk. Test it. Be careful. 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.