Wordpress Archiver

In my quest to consolidate and shutdown old servers, I found a Wordpress site that I had take offline several years ago. The site itself dated from 2007 or so, and had some sentimental value. So, I wanted to figure out a decent way to archive it.

This particular site didn't(?) use a WYSIWYG, so posts were pretty simple, and resembled Markdown. First, I created a simple composer.json:

    "require": {
        "michelf/php-markdown": "^1.8"

Then I wrote a simple script in PHP:


require 'vendor/autoload.php';

use Michelf\Markdown;

$timezone = new \DateTimeZone('America/New_York');
$all = '';

$pdo = new \PDO('mysql:host=localhost;dbname=the_database', 'username', 'password');
$stmt = $pdo->query('SELECT * FROM wp_posts WHERE post_status = "publish" ORDER BY ID ASC');

while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  $ID = (int) $row['ID'];

  $post_date_gmt = new \DateTime($row['post_date_gmt'] . ' GMT');

  $markdown = '## ' .  $row['post_title'] . PHP_EOL . PHP_EOL;
  $markdown .= '### ' .  $post_date_gmt->format('r') . PHP_EOL . PHP_EOL;
  $markdown .= $row['post_content'];

  $filename = sprintf('%03d--%s.md', $ID, $row['post_name']);
  file_put_contents($filename, $markdown);

  $html = Markdown::defaultTransform($markdown);
  $all .= $html . PHP_EOL . PHP_EOL;

  $filename = sprintf('%03d--%s.html', $ID, $row['post_name']);
  file_put_contents($filename, $html);

$all = "# My Old Blog\n\n{$all}";
file_put_contents('000--all.html', $all);

I ran the script from the command line, and ended up with a Markdown and HTML file for each published post I had, as well as an HTML file for everything, minus the theme. Easy Peasy.