Skip to content

Instantly share code, notes, and snippets.

@chris-79
Created May 3, 2016 15:23
Show Gist options
  • Save chris-79/c8d539122c43661d1244447e089899bf to your computer and use it in GitHub Desktop.
Save chris-79/c8d539122c43661d1244447e089899bf to your computer and use it in GitHub Desktop.
Wordpress exported xml to markdown entries
<?php
// USAGE: php magic.php
// Edit the details below to your needs
$wp_file = 'wordpress-export.2015-11-23.xml';
$export_folder = 'test/'; // existing files will be over-written, use with care
print "Exporting contents from $wp_file to folder\"$export_folder\"...\n";
if (file_exists($wp_file)) {
$xml = simplexml_load_file($wp_file);
$count = 0;
foreach ($xml->channel->item as $item) {
$count ++;
print "Exporting: (".$count.") " . $item->title."\n";
$title = $item->title;
$tags = array();
$categories = array();
$item_date = strtotime($item->pubDate);
$file_name = $export_folder.date("Y-m-d-Hi", $item_date).".".slugify($title).".md";
if ($title == '') {
$title = 'Untitled post';
}
foreach($item->category as $taxonomy) {
if ($taxonomy['domain'] == 'post_tag') {
$tags[] = $taxonomy['nicename'];
}
if ($taxonomy['domain'] == 'category') {
$categories[] = $taxonomy['nicename'];
}
}
print " -- filename: ".$file_name;
$markdown = "---\n";
$markdown .= "title: '" . $title ."'\n";
if (sizeof($tags)) {
$markdown .= "tags:\n - ".str_replace(",","\n - ",str_replace("-", " ", implode(",", $tags)))."\n";
}
if (sizeof($categories)) {
$markdown .= "categories:\n - ".str_replace(",","\n - ",str_replace("-", " ", implode(",", $categories)))."\n";
}
$markdown .= "---\n";
$markdown .= $item->children('content', true)->encoded;
$markdown .= "\n";
file_put_contents($file_name, $markdown);
print "\n";
}
} else {
print "File does not exist. Exiting\n\n";
}
// Credit: http://sourcecookbook.com/en/recipes/8/function-to-slugify-strings-in-php
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
if (function_exists('iconv'))
{
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
{
return 'n-a';
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment