Created
March 21, 2014 00:02
-
-
Save tylerhall/9676748 to your computer and use it in GitHub Desktop.
Import a Jekyll posts directory into WordPress
This file contains hidden or 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 | |
require '/path/to/markdown-extra.php'; | |
$db = mysql_connect('localhost', 'root', 'password') or die(mysql_error()); | |
mysql_select_db('tylerio', $db) or die(mysql_error()); | |
$files = scandir('posts'); | |
array_shift($files); // . | |
array_shift($files); // .. | |
foreach($files as $fn) | |
{ | |
import_md($fn); | |
} | |
function import_md($fn) | |
{ | |
global $db; | |
$md = file_get_contents('posts/' . $fn); | |
$lines = explode("\n", $md); | |
$dashes = array_shift($lines); | |
$tmp = explode(':', array_shift($lines)); | |
$date = $tmp[1] . ':' . $tmp[2] . ':' . $tmp[3]; | |
$date = date('Y-m-d H:i:s', strtotime($date)); | |
$title = trim(array_pop(explode(':', array_shift($lines)))); | |
$layout = array_shift($lines); | |
$permalink = trim(array_pop(explode(':', array_shift($lines)))); | |
$slug = trim(array_pop(explode(':', array_shift($lines)))); | |
$dashes = array_shift($lines); | |
$body = implode("\n", $lines); | |
$body = str_replace('{{ site.cdn_url }}', 'http://cdn.clickontyler.com', $body); | |
$title = mysql_escape_string($title); | |
$body_md = mysql_escape_string($body); | |
$body_html = mysql_escape_string(Markdown($body)); | |
$permalink = str_replace('index.html', '', $permalink); | |
echo $permalink . ' ' . 'http://tyler.io/blog' . $permalink . "\n"; | |
$sql = "INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_status, comment_status, ping_status, post_name, post_modified, post_modified_gmt, post_parent, post_type) VALUES "; | |
$sql .= "(1, '$date', '$date', '$body_html', '$body_md', '$title', 'publish', 'closed', 'open', '$slug', '$date', '$date', 0, 'post')"; | |
mysql_query($sql, $db); | |
$id = mysql_insert_id($db); | |
mysql_query("UPDATE wp_posts SET guid = 'http://tyler.io/?p=$id' WHERE ID = $id", $db); | |
mysql_query("INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES ($id, '_sd_is_markdown', '1')", $db); | |
} |
Thanks, but how could I use this script ? should I add it as a plugin?
Here's the accompanying blog post (not currently linked). I found it interesting he moved back to WordPress but kept the Disqus commenting system. I'd really like to understand why that decision was made.
Those who're looking to move back to WordPress for [insert reason here] from another blogging platform (doesn't have to be Jekyll), the Web gave us a beautiful thing called the RSS specification. Here's a plugin from @kemayo for performing RSS post imports and the story behind why he created it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hej @tylerhall did the markdown-extra.php is given by wordpress?