How to migrate your own blog to WordPress

Recently, I migrated from my own blogging software to WordPress.
It was a reasonably straightforward process, but there were a few caveats, and I thought I’d share my experiences with you, so you can do it even faster than I did.

Here is my custom blog to WordPress migration script.
You will probably not be able to do much with it directly, but it gives an example of how it might be done and illustrates a few points.
It should probably be noted that the functions db_query() and db_query_die() are just wrapper functions for mysql_query().

Step 1: Install WordPress

This was as easy as it can be.
Once I had WordPress running, I could take a look at the database structure for WordPress.

Step 2: Find the corresponding WordPress tables and columns

I took a look at the WordPress database, and found out that the comments table was called wp_comments (I used the default table prefix) and the posts table was called wp_posts.

I took at look at what columns were in these tables, and made a little list of how my columns mapped over to the wp columns.
You can take a look at the supplied source file. There are two comments blocks that show how my own table columns map over to the comments and posts tables.

Step 3: Read, convert, write

Write code that

  • Reads from your database,
  • Converts things like charsets, timestamp formats and paths, and
  • then writes into the WordPress tables.

Before writing the code, look through the list of caveats below and avoid making some of the mistakes I did.

Caveats

My own blog software had threaded comments, and no categories.

WP doesn’t have threaded comments
I took a look for a threaded comments hack, so I could find a compatible column name for the needed column.
The column name used by several threaded comments plugins/hacks is comment_reply_ID.
I simply added this column to the wp_comments table.

WP doesn’t have titles on comments
Which is understandable, since hardly anyone ever uses it.
But I didn’t want to throw the data away, so I merged it into the comment body, by making it bold and adding a line break after it.
It’s done with this code:

if($col_title)
$body = "<strong>$col_title</strong>\n<p />\n$col_comment";
else
$body = $col_comment;

WP uses post names
WordPress uses a sanitized version of the title to make nice links to your blog entries.
I missed this at first, and even though I could see all my entries, I couldn’t click on the comments page for them, because that was generated using the post_name.
I took a look at what WP did when storing a post in the database, and found out it uses a function called sanitize_title_with_dashes(). I used this to make post names, in the line:

$col_post_name = sanitize_title_with_dashes($col_title);

WP has an extra timestamp column for GMT for posts and comments
I needed to calculate the value for that column from my own basic timestamp column and my knowledge of my own GMT offset.
The easiest thing is to convert everything to UNIX epoch format (seconds since 1st of January 1970, as far as I remember), and then do the calculations in seconds.
I am doing that in the line:

$col_date_gmt = $unix_date - 60*60;

I subtract 1 hour (60*60 seconds) from my normal timestamp, because I am in Denmark at GMT+1 hour.

Associating each post with a default category
The table wp_post2cat holds relations between categories and posts.
I made sure all posts were added to the default category with the line:

db_query_die("INSERT INTO wp_post2cat (post_id, category_id) VALUES ($col_id, 1);");

You could probably do something smarter here, like sorting them using keywords or use the categories from your own blog, if it has any.

Using relative paths will break with WPs nice mod_rewrite URLs
WP can use mod_rewrite to convert long, ugly query string to nice URLs.
One of the drawbacks to this is, that relative URLs from the post will most likely not work.
So in my migration script, I converted all the relative image paths to absolute paths with the line:

$col_entry = str_replace("images/", "/blog/images/", $col_entry);

And that’s it.
If you have links, categories and other things in your own blogging system, there may of course be more problems, but for your basic comments-and-posts blogging system, this should suffice.

I actually wrote the migration script as a test, just to see how easy it would be to migrate to WP if I ever wanted to. It turned out it was so easy and WordPress was so nice that my trial run ended up being the actual migration.

If you are thinking about switching to WordPress from your own blogging system, I can happily tell you that my only regret is that I didn’t do it sooner. And if you have the skills to write your own blog, you can probably also write some really nice plugins for WordPress that will gain you fame, fortune and sexy women.
Or at least some really appreciative comments on your blog.

Good luck.