Skip to content

Instantly share code, notes, and snippets.

@tylerhall
Created March 21, 2014 00:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tylerhall/9676748 to your computer and use it in GitHub Desktop.
Save tylerhall/9676748 to your computer and use it in GitHub Desktop.
Import a Jekyll posts directory into WordPress
<?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);
}
@jalogisch
Copy link

Hej @tylerhall did the markdown-extra.php is given by wordpress?

@banpie
Copy link

banpie commented Feb 7, 2017

Thanks, but how could I use this script ? should I add it as a plugin?

Copy link

ghost commented Jun 18, 2017

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