Created
August 30, 2011 19:11
-
-
Save chelmertz/1181744 to your computer and use it in GitHub Desktop.
wordpress to jekyll export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// The following is modified from https://raw.github.com/tylerhall/clickontyler.com/master/_scripts/import.php by Tyler Hall | |
// 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('Europe/Berlin'); | |
$database_host; | |
$database_user; | |
$database_password; | |
$database_name; | |
$db = mysql_connect($database_host, $database_user, $database_password); | |
mysql_select_db($database_name, $db); | |
// Export posts... | |
$posts = array(); | |
$results = mysql_query("select * from 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 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']; | |
} | |
$tags_sql = mysql_query("select t.slug | |
from terms as t | |
inner join term_taxonomy as tt | |
on tt.term_id = t.term_id | |
and taxonomy = 'post_tag' | |
where tt.term_id in ( | |
select term_taxonomy_id from term_relationships where object_id = ".$row['ID']." | |
)", $db) or die(mysql_error()); | |
$tags = array(); | |
while($tags_row = mysql_fetch_array($tags_sql, MYSQL_ASSOC)) | |
{ | |
$tags[] = $tags_row['slug']; | |
} | |
$row['tags'] = $tags; | |
$posts[] = $row; | |
} | |
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"; | |
// faster than figuring out how yaml escapes quotes | |
if(strpos("'", $p['post_title']) === false) { | |
$out .= "title: '".$p['post_title']."'\n"; | |
} elseif(strpos('"', $p['post_title']) === false) { | |
$out .= 'title: "'.$p['post_title']."\"\n"; | |
} else { | |
$out .= 'title: '.$p['post_title']."\n"; | |
} | |
$out .= "layout: post\n"; | |
$out .= "slug: '$slug'\n"; | |
if($p['tags']) { | |
$out .= "tags:\n- ".implode("\n- ", $p['tags'])."\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.md", $out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment