Mudita Journal

Importing from MT to WordPress

March 29, 2006 · Filed under: Web Development

UPDATE: (Oct 23, 2010) Please read the comments below, especially the ones toward the end, for some relevant warnings about the latest versions of WordPress.

This week I spent several hours figuring out how to import all my entries from MoveableType to WordPress.

It turns out the task is non-trivial when you’re converting to WordPress 2.0 or higher (highly recommended) and you want to preserve the numeric permalinks of your posts.

If you want to keep your old permalinks alive for search engine purposes, and you’ve been using MT’s numeric filenames, then it’s crucial that you preserve the numbering assigned to your old entries.

If you conduct an ordinary import, however, any of the posts you had deleted in MT (e.g., test posts or any posts you decided to take down) will not get counted during WP’s import, causing the numeric IDs of your new posts to not match up with the old ones.

I was able to piece together a solution that worked, however. Below are my instructions.

1. Bone up on the general topic by reading WordPress’s own documentation on importing from MT to WordPress.

2. Edit MT’s export script to include post numbers. I don’t know the first thing about perl, but fortunately Dr. Bacchus provided some crucial guidance on this:

The module that handles MT exports is at lib/MT/App/CMS.pm. In sub export you’ll find a blob of text that starts with $tmpl->text(< <'TEXT'); and defines the template with which stuff will be exported. To that blob, add a new field:

ID: <$MTEntryID$>

Placement doesn’t matter. Save the file and upload it into your old MT installation.

3. Export your entries from MoveableType. Save the exported text file to your hard drive for later use. You might want to peek at the file in a text editor, to confirm that the headers for your posts include the numeric ID for that post.

4. Update WordPress’s script for importing entries. Find the file mt.php inside the folder /wp-admin/import, and rename the file to something like mtORIG.php (for backup purposes).

Then replace the file with this one. In the new version of this file, I’ve altered the function that updates the database, to include the post number when it does the update. (For esoteric reasons, this is not a trivial modification in WordPress v.2. They’re no longer using conventional SQL queries, for one thing.)

NOTE: I am not an experienced WordPress programmer, and I can’t make any guarantees about the fitness of my patch. If anyone with more experience sees problems with my script, please send them along and I’ll update my files accordingly. That said, the script worked like a charm for me — and for another person in the support forums who was having a similar problem.

5. Import your entries into WordPress. Once you’ve uploaded the revised import script into your WordPress installation, you should be able to import just like you normally would, inside the WordPress admin section: Click on the “Import” tab, choose “MoveableType,” and follow the instructions.

BONUS: There’s one other challenge when preserving your numeric permalinks, and that’s padding the zeroes in front of the post ID in the URL. Fortunately Kafkaesqui provides the solution: Edit the template-functions-links.php file (inside the /wp-includes folder). Look for the $rewritereplace array, and in it change:

$post->ID,

to:

zeroise($post->ID, 6),

That uses the WP function zeroise() to force the ID to 6 places by inserting zeros at left when it’s less than 6 digits.

Hope these instructions are helpful. I am available to answer questions on a limited basis. Please post your questions in the comments, below, so that others can benefit from the discussion.

  • http://www.danielneto.com.br/weblog/ Daniel Neto

    Thanks for these steps! It worked for me perfectly!

  • Pingback: tarun upadhyay

  • http://www.jaymis.com Jaymis

    This plugin seems to do some kind of code filtering which is causing rather strange things to happen for me. I’ve done a search and replace on my MT plaintext export file so I can use images with the Gallery2 integration WPG@. This means my image links look like this:

    storiespre2k6/soundframes.jpg

    However after importing with this script they look like this:

    storiespre2k6/soundframes.jpg

    For some reason the import eats the wpg2 tags and the div class.

    I’ve successfully imported this stuff using Kathy’s Numeric Import Script – but because it’s based on WP1.5 it requires quite a lot of work to get everything happy after import. All comments need to be moderated and approved to update the post comment counts etc.

    So, it looks like your script would be a much better option, but for some reason it doesn’t seem to be working. It does the import beautifully, but this filtering breaks most of the posts, and the numeric IDs don’t seem to be correct.

    I may have done something strange so I’m going to give this another try, but at the moment it’s a litany of badness :)

    Jaymis

  • http://www.zader.com Joshua Zader

    Jaymis,

    I saw your follow-up post (with the code that got eaten, above) but I had to delete because it broke my page; still not sure why it happened, but it was easiest to just remove it.

    I’m sorry to hear my script didn’t work for you. I don’t have any experience with the Gallery2 integration code you’re using, so unfortunately I don’t have any ideas about how to fix it.

    My hunch is that it’s not my modifications (which were mostly minor tweaks to a large chunk of their code that defines one of their functions), but rather something in how the new version of WordPress is filtering your imports.

    But I dunno. Hope you’re able to figure it out without too much heartache.

    Joshua

  • http://www.sadlyno.com Seb

    Hmm, maybe I’m crazy, but my version of MT (3.2)’s CMS.pm has a “sub export” section but no “blob of text that starts with $tmpl->text.” What version of MT were you using? (I do see the section you indicate in the CMS.pm version that came with 2.661.)

    Answer from Joshua: I don’t know what version I was using, or what to do with a different version of MT.  Sorry.

  • http://www.sadlyno.com Seb

    Joshua: thanks. Looks like, at least for purposes of exporting posts (forget templates and so on,) importing a 3.2 DB into a 2.6 install works well enough.

  • Pingback: Odysseygate.com

  • Albert

    I migrated from Movabletype 3.31 to WordPress 2.03. Everything worked as described except that the code that has to be updated in the MT export script is now in lib/MT/ImportExport.pm (~line 529) not lib/MT/App/CMS.pm.

  • Pingback: Dan Moore! » Moving to WordPress

  • http://www.joeldown.org Joel Down

    Joshua

    Thanks for these very helpful tips. If others are, like me, exporting from 3.2 then note you’ll want to make the same change as at 2. above, but in

    lib/MT/ImportExport.pm

    and the phrase you’ll want it

    ID:

    Worked for me, anyway.

  • http://fistfulofeuros.net Tobias Schwarz

    Hi,

    I’ve recently adapted your solution to add MT’s BLOG_ID as a category to WP and thus allow the preservation and handling of previous multiblogs as WP categories.

    To do this, you would have to add an additional BLOG_ID in step 2, like

    BLOG_ID:

    and then an additional category importer step in the modified WP MT import script. I put this just below the catgory import case:

    case ‘BLOG_ID’ :
    if (! empty ($value) )
    $post_categories[] = $wpdb->escape($value);
    break;

    Unfortunately, WP can’t handle primary categories without a plugin, but there is one – top categories.

    If I’d known this before I imported the posts, I’d have installed it prior to importing the posts and adapted the script even further so the the MT_PRIMARY_CATEGORY value would have been preserved.

  • Pingback: Technology for Social Change » Blog Archive » Moving to Wordpress

  • http://www.kempa.com Adam

    I think the file to alter in the Bonus section of Step 5 has changed to wp-includes/link-template.php

  • http://www.giornale.fm Giornale

    ..thanks a lot for the useful post!

  • http://blog.iamnotachef.com Daniel Lapin

    I tried your fix (replacing the mt import file with your version) but it did not work. The weird problem I am having is that all of the comments import correctly, and I even get the “Have Fun” message at the bottom of the list of posts, but the posts do not show up! However, when I look at the comments and click on a post name, the post appears as it would normally. I don’t get it.

    I have tried reinstalling everything but to no avail. And when I re-import, I get a list of the posts with a notation at the end of each name that it “already exists.”

    Weird, huh!

    Any suggestions?

    Thanks.

  • Pingback: TBOTCOTW » Blog Archive » Ch-Ch-Ch-Changes!

  • http://www.banlieusardises.com Martine

    Hello! I know it’s been a long time since you wrote this, but maybe you’ll be able to help.

    All goes well for me until I get to the importing part, with the mt.php modified script. In my case, the entries import fine, it’s the comments that don’t show up! They imported just fine with the original mt.php script.

    Any thoughts on this? I have many years of blogging in archives. Preserving my permalinks is crucial… but also the comments people took time to write over the years…

  • http://www.zader.com Joshua Zader

    Martine,

    Unfortunately, I don’t have any ideas. :-( It’s been three years since I worked on the import script, so I’m surprised it still works at all!

    Hopefully someone who knows PHP can help you update the script to suit your purposes.

    If anyone does create an updated version of the script, let me know here and I’ll post a prominent link.

    Joshua

  • http://www.banlieusardises.com Martine

    Thanks anyway for your answer! In case this can help someone else: just after posting my question, I tried some kind of desperate move… I reimported again using the original mt.php. Guess what? It worked: the comments got associated to the original posts, preserving the ID that previously was imported with your modified mt.php script.

    The only problem now: the Dashboard seems to have lost the capability to count the posts. It shows “0 post”, even though the posts are there, published, and are counted in category listings.

    Even new posts aren’t counted: the Dashboard always shows 0. I just don’t know if this is an issue that could lead to further problems… :-(

  • http://www.chrisbrunner.com/ Chris Brunner

    Everything here has worked wonderfully, so thank you.

    My only problem is that your version of mt.php doesn’t include a way to import a local file, and my mt-export.txt file is 26MB.

    So, I’ll have to either adjust my timeout values and try to upload my file through the web interface or code that functionality back into mt.php.

    In any case, you may want to modify your post to warn people of this.

  • Chris

    Has anyone tried this recently? I have this exactly problem I’m trying to tackle.

  • Anonymous

    As the Movable Type importer is somewhat different now than when you made your changes to the import script, I updated it to accept the ID field exported from Movable Type as per your instructions.

    It’s actually much simpler now–the wp_insert_post() function now accepts an “insert_id” field, which is a suggested id for the newly created post, which will be used in the case that it is not already in use by another post. Also, the importer script is now a plugin, so after installing the plugin, you have to replace the existing file at wp-content/plugins/movabletype-importer/movabletype-importer.php with this one:

    http://davidcanderson.net/files/movabletype-importer.php

  • yowzadave

    As the Movable Type importer is somewhat different now than when you made your changes to the import script, I updated it to accept the ID field exported from Movable Type as per your instructions.

    It’s actually much simpler now–the wp_insert_post() function now accepts an “insert_id” field, which is a suggested id for the newly created post, which will be used in the case that it is not already in use by another post. Also, the importer script is now a plugin, so after installing the plugin, you have to replace the existing file at wp-content/plugins/movabletype-importer/movabletype-importer.php with this one:

    http://davidcanderson.net/files/movabletype-importer.php

  • Kevin Murphy

    David C. Anderson’s fix has disappeared. So some new intel would be much appreciated, if anyone has it.

  • Kevin Murphy

    David C. Anderson’s fix has disappeared. So some new intel would be much appreciated, if anyone has it.

  • Kevin Murphy

    Ok, here’s the new way to do it. I did this this afternoon and it works.

    http://www.analogousmember.com/archives/516

    You may have to scrub your XML for funky characters. The way this post suggests makes it relatively easy.

    Also, if like me, you needed to add two zeros to your WP posts to keep the numbers right as MT, just use the simple 301 redirect plug in. (00%postid% –> %postid%) Inelegant, and I’m surprised it worked, but it seems to be doing the trick.

    • http://www.zader.com Joshua Zader

       Thanks for the update, Kevin!

  • Kevin Murphy

    Ok, here’s the new way to do it. I did this this afternoon and it works.

    http://www.analogousmember.com/archives/516

    You may have to scrub your XML for funky characters. The way this post suggests makes it relatively easy.

    Also, if like me, you needed to add two zeros to your WP posts to keep the numbers right as MT, just use the simple 301 redirect plug in. (00%postid% –> %postid%) Inelegant, and I’m surprised it worked, but it seems to be doing the trick.

    • http://www.zader.com Joshua Zader

       Thanks for the update, Kevin!