Importing from pyblosxom to WordPress

Importing to WordPress from [py]blosxom is not as easy as it could be. I ended up writing some PHP to loop over my posts and call wp_insert_post() for each one. Nifty, almost, except that preserving a category hierarchy is a bear.

One massive gotcha: the post bodies are sanitized on import so if you’re using something like Markdown for formatting you get a real mess when it “helpfully” converts > characters to HTML entities, swallows URLs properly enclosed in <>, etc. There is a workaround, thankfully:

#
# The WordPress post function sanitizes all input, and this
# includes escaping bare '>' characters to HTML entities.
# This is most unhelpful when importing Markdown data.
#

    kses_remove_filters();

and then you can safely call insert without your content getting munged:

#
# Construct post and add.
#

    $post = array(
            'post_title' => $title,
            'post_name' => $slug,
            'post_content' => $content,
            'post_category' => array($category),
            'post_date' => $date,
            'post_status' => "publish",
            'post_author' => 2,
    );      

    $result = wp_insert_post($post), true);

    if (is_wp_error($result)) {
            echo "ERROR\n";
            echo print_r($result);
    } else {
            echo "New post ID: $result\n";
    }

etc.

Like I said, it’s a royal pita to figure out the category ID; you need to use the raw ID numbers from the database, not symbolic names or slugs. Ugh.

My blog’s RSS feed is now http://blogs.operationaldynamics.com/andrew/feed. I did my best to preserve post times, guids, etc, but there came a point where it just wasn’t going to get any closer; sorry if you get dups in your reader or planet.

Incidentally, what we all knew as “WordPress MU” is now called a “Network Install”. Go figure, but if you need multi-site aka multi-blog aka multi-user installation you really need to read “Create A Network” for instructions. Do this setup before creating (or importing) content unless you want the joy and bliss of reinstalling several times.

AfC