Skip to content

Instantly share code, notes, and snippets.

@mirhec
Forked from vlucas/markdown-import.php
Created April 21, 2021 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirhec/801cbe4868c968f616d82dff1625b0b5 to your computer and use it in GitHub Desktop.
Save mirhec/801cbe4868c968f616d82dff1625b0b5 to your computer and use it in GitHub Desktop.
Imports Markdown files in a local directory into a WordPress installation
<?php
// Turn on all error reporting so we can see if anything goes wrong
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);
// Relative path to your wp-config.php file (to connect to the database)
require '../wp-config.php';
require './frontmatter.php'; // Parses YAML frontmatter - https://github.com/Modularr/YAML-FrontMatter
require './Parsedown.php'; // Markdown parser - https://github.com/erusev/parsedown
$parsedown = new Parsedown();
// CHANGE TO YOUR SITE
$site_domain = 'http://vancelucas.com';
$markdown_file_relative_dir = 'blog/';
$db = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
mysql_select_db(DB_NAME, $db) or die(mysql_error());
$files = scandir($markdown_file_relative_dir);
array_shift($files); // .
array_shift($files); // ..
// OPTIONAL: Delete all posts beforehand - useful for tweaking this script to get it just right until it works
//delete_all_posts($db);
foreach($files as $fn) {
import_md($fn);
}
function delete_all_posts($db) {
mysql_query("TRUNCATE wp_posts", $db);
mysql_query("TRUNCATE wp_postmeta", $db);
}
function import_md($fn) {
global $db, $parsedown, $markdown_file_relative_dir;
$mdFile = __DIR__ . '/' . $markdown_file_relative_dir . $fn;
$md = file_get_contents($mdFile);
$post = new FrontMatter($md);
$title = trim($post->fetch('title'), "'\" ");
$date = $post->fetch('date');
$date = date('Y-m-d H:i:s', strtotime($date));
$body = $post->fetch('content');
$tags = explode(',', str_replace(array("'", '[', ']'), '', $post->fetch('tags')));
// Slug is filename - remove date from beginning, and extensions from end
$slug = substr($fn, 11);
$slug = preg_replace('/-+/', '-', substr($slug, 0, strpos($slug, '.')));
// Build full permalink
$permalink = $site_domain . '/blog/' . $slug . "\n";
// Replace 'READMORE' with WordPress equivalent
$body = str_replace('READMORE', '<!--more-->', $body);
$title = mysql_escape_string($title);
$body_md = mysql_escape_string($body);
$body_html = mysql_escape_string(str_replace(array("\r\n", "\r", "\n"), " ", $parsedown->text($body)));
echo 'Imported: ' . $permalink . " (tags: " . implode($tags, ',') . ")<br />\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);
wp_set_post_tags($id, $tags, false);
mysql_query("UPDATE wp_posts SET guid = '$site_domain/?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);
}
@mirhec
Copy link
Author

mirhec commented Apr 24, 2021

In order to use this snippet, upload it to the server and run composer require modularr/yaml-front-matter in the directory. Furthermore download Parsedown.php and place it in the same directory. The blog content goes in the subfolder ./blog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment