January 21, 2013 / Web/
I was recently working on a website upgrade project where we were replacing a Movable Type site with a brand new shiny WordPress site. We needed to migrate over 1,300 posts from the old site to the new one. The import and export tools provided with both platforms was almost sufficient, except for one caveat:
Both the old and new sites used custom fields (Pro Pack in MT, standard in WP) to store data such as MP3 links, images, and so forth. If you use the standard Movable Type export feature, you get a lovely big text file which contains almost everything post-related, except for these custom fields. As such, the WordPress import tool had no support either.
Luckily, it was fairly easy process to work around this limitation by getting our hand dirty in the source code. To complete this you’ll need to be comfortable with programming in PHP. If not, find a competent friend, co-worker, or contractor (such as myself).
Here we go…
Firstly, we needed to export these fields we want from Movable Type. Within the MT Web GUI, we can go to Preferences > Custom Fields to find the names of these fields. If we open up each one and take a look under the “Example Template Code” section we can find a template tag in the format similar to “<mt:entrydatafield_name>”. Make note of these.
Next, load up your FTP client and download the file /cgi-bin/lib/MT/ImportExport.pm. We are going to edit this Perl script, so load it in your favourite text editor (try Notepad++ on PC or TextWrangler on Mac). Scroll down to about line 480. Here, you will find the export template, starting with $tmpl->text(<<‘TEXT’);. This is what is written to the exported text file.
Edit this to contain your fields. Create an entry for each custom field in the same format as the others, using the template tags you noted earlier. Make sure line breaks are correct.
If you save and upload this to the server, you can now export the posts through the web GUI. Open this up in your text editor and see if your new fields are here.
If you’ve got this far, it’s time to edit the import script in WordPress. Log into WordPress and go to Tools > Import. If you haven’t already, choose to download the Movable Type import tool. Now, load up your FTP client and download /wp-content/plugins/movabletype-importer/movabletype-importer.php.
Load this up in your text editor and scroll down to the process_posts() function on line 334. If you keep going a bit further, you’ll find a while loop which loops through every line of your exported text file. There is a massive If/Else block which deals with each line and each field. We are going to add another If/Else condition for each field we want to import.
Copy and paste one of the existing blocks and modify it to suit your field. Your code should probably look something like this:
You can also get clever and rewrite parts of the fields here. For example, I rewrote a whole heap of URLs so they point at their new location. I also edited some other fields to achieve some basic formatting consistency. Functions such as str_replace() and preg_replace() are your friend here.
Next, we need to edit the save_post() function to actually write these fields to the database. In this function, add some basic conditionals to check the existence of values and then use the update_post_meta() function to write the values. As you can see, we use the $post_id and the other variable we set earlier to write these meta values.
Side note: Any custom field in WordPress starting with an underscore will not be visible to the user in the “Custom Fields” panel in the Post editor. You can use these via custom plugins.
With any luck, you should be done. Upload the script to the server, and test it out. I suggest you don’t do this on a production server. Also, it may be best to cut down the import text file to contain fewer posts if it is very large. This will speed up the process as you debug any problems.
Hopefully this works for you, although the process may vary slightly from version to version.
Get the Broadcast Technology Newsletter
Sign up for the email newsletter about media and technology. Sent irregularly. No spam.