Skip to content

Instantly share code, notes, and snippets.

@stefanbc
Created January 8, 2013 20:19
Show Gist options
  • Save stefanbc/4487560 to your computer and use it in GitHub Desktop.
Save stefanbc/4487560 to your computer and use it in GitHub Desktop.
If you need to migrate your blog from Wordpress to Jekyll there is a PHP code for it.
<?php
// This is the script I used to export my old blog out of WordPress and into a
// Jekyll friendly format. The first half exports posts, the second does pages.
// It's fairly specific to my needs, but maybe it'll be a good starting point
// for you. /RTH 2011-08-28
// https://twitter.com/#!/marcoarment/status/59089853433921537
date_default_timezone_set('America/Los_Angeles');
$db = mysql_connect('host', 'username', 'pass') or die(mysql_error());
$d = mysql_select_db('dbname', $db) or die(mysql_error());
// Export posts...
$posts = array();
$results = mysql_query("select * from wp_posts where post_type = 'post' and post_status = 'publish'") or die(mysql_error());
while($row = mysql_fetch_array($results, MYSQL_ASSOC))
{
$results2 = mysql_query("select meta_key, meta_value FROM wp_postmeta where post_id = " . $row['ID'], $db) or die(mysql_error());
while($row2 = mysql_fetch_array($results2, MYSQL_ASSOC))
{
$row[$row2['meta_key']] = $row2['meta_value'];
}
$posts[] = $row;
}
mkdir('posts', 775);
foreach($posts as $p)
{
$slug = $p['post_name'];
$dt = $p['post_date_gmt'];
$y = date('Y', strtotime($dt));
$m = date('m', strtotime($dt));
$out = "---\n";
$out .= "date: $dt\n";
$out .= "title: {$p['post_title']}\n";
$out .= "layout: post\n";
$out .= "permalink: /blog/$y/$m/$slug/index.html\n";
$out .= "slug: $slug\n";
$out .= "---\n";
$out .= $p['post_content'];
$dt_slug = date('Y-m-d', strtotime($dt));
echo "Exported Post: $slug\n";
file_put_contents("posts/$dt_slug-$slug.markdown", $out);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment