-
-
Save jasonbrooks/6a52bff5d484ceb3350b09af3637e79f to your computer and use it in GitHub Desktop.
Imports Markdown files in a local directory into a WordPress installation
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 | |
// 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(); | |
require 'vendor/autoload.php'; | |
// CHANGE TO YOUR SITE | |
$site_domain = 'https://blogs.rdoproject.org'; | |
$markdown_file_relative_dir = 'blog/'; | |
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysqli_connect_error()); | |
mysqli_select_db($db, DB_NAME) or die(mysqli_connect_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) { | |
mysqli_query($db, "TRUNCATE wp_posts"); | |
mysqli_query($db, "TRUNCATE wp_postmeta"); | |
} | |
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'))); | |
$author = trim($post->fetch('author'), "'\" "); | |
// 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 = mysqli_real_escape_string($db, $title); | |
$body_md = mysqli_real_escape_string($db, $body); | |
$body_html = mysqli_real_escape_string($db, str_replace(array("\r\n", "\r", "\n"), " ", $parsedown->text($body))); | |
// Create users | |
wp_create_user($author, 'password'); | |
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, post_excerpt, to_ping, pinged) VALUES "; | |
$sql .="((SELECT ID from wp_users WHERE user_login = '$author'), '$date', '$date', '$body_html', '$body_md', '$title', 'publish', 'closed', 'open', '$slug', '$date', '$date', 0, 'post', 0, 0, 0)"; | |
print $sql; | |
mysqli_query($db, $sql); | |
$id = mysqli_insert_id($db); | |
wp_set_post_tags($id, $tags, false); | |
mysqli_query($db, "UPDATE wp_posts SET guid = '$site_domain/?p=$id' WHERE ID = $id"); | |
mysqli_query($db, "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES ($id, '_sd_is_markdown', '1')"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The dates only worked for posts w/ a TZ in the
date:
frontmatter. I usedsed -i -E 's/(date: ....-..-.....:..:..$)/\1 UTC/g' blog/*
to add UTC as a TZ to posts missing that.