In the almost two months we’ve spent in Coronavirus lockdown here in Venice, I’ve had the opportunity to get some of the old forgotten tasks done. During one of these I finally got around working with WordPress from the command line, and directly from PHP without running a web server.
It is amazingly simple. My server is running Debian, and this code is all that’s needed to run WordPress from the command line:
// The site you want to access using Debian's multisite
$_SERVER['HTTP_HOST'] = 'wpsights.seindal.dk';
// Location of WP installation on Debian
define('ABSPATH', '/usr/share/wordpress/');
// Load the beast
define('WP_USE_THEMES', false);
require_once(ABSPATH . 'wp-load.php');
After this WordPress is loaded for the site given by HTTP_HOST and you can do anything you want with the WordPress API.
Reading a JSON file and inserting 1200 pages in a hierarchy becomes just a few dozens lines of code.
The background
The task at hand was migrating a very old site to something more recent. The site in question, sights.seindal.dk, was made almost twenty years ago, and had been abandoned for at least fifteen years. Yet, back then I’ve put in a lot of effort, and the site did have some decent traffic still.
The old site was programmed in Perl using the Mason framework, with a mysql backend. The new site would be WordPress because it’s ubiquitous and fairly easy to work with, and I have a bunch of other sites on WP.
There were some 1200 pages on the site, and about 10,000 photos, all meticulously tagged and cross-indexed. Many articles are quite substantial. Everything was cross-linked automatically on the site. I wanted all this moved over, and preferably not by hand.
In the end I modified the old perl code to export the pages and photo metadata as JSON, and wrote various small scripts in PHP to read the JSON and load everything into WordPress as pages, media, tags and categories.
Given the ease of using WordPress directly from the command line, I could make many small scripts and do the migration a bit at a time.
Add to that a child theme of a very basic free theme, and a content filter to do the automatic linking, and the task was done.
It took about a week to get all the data over, and another week to do the child theme and automatic linking.
Leave a Reply