Last active
May 19, 2024 17:58
-
-
Save vlucas/92567cb04668c0c87a52f15764ab95e1 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(); | |
// 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); | |
} |
Do all the markdown files need to be in the same folder? Or can they be in a subfolders for example I have Hugo blog and the blog are broken down into a folder by the month and year. Our goal is to convert our Hugo site to wordpress.
This is kinda vague "Tweak some settings to specify your domain and directory" Which file do we have to tweak?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
markdown-import.php
) to the same place as your markdown filesmarkdown-import.php
)markdown-import.php
in your browser).