Migrating Content to WordPress with a Custom Plug-in
Version Information: This was written based on WordPress 4.6.1
I recently was tasked with migrating content from a couple of very old Drupal sites to WordPress. One was Drupal 5 and one was Drupal 6. Using the Digital Ambit Data Integration Framework and mimicking the design patterns used in the more modern Drupal Migration API I was able to put together a dirty plug-in that I have put up on github.
It uses a custom mysql table to track what content has been migrated successfully. This allows me to do the migration in batches and also allows me to create external processes that act on the data. I used external processes to update URLs to new patterns, create .htaccess redirects, and replace old image links with new image links.
The code portion of the project hinges on WordPress' plug-in system and the wp_insert_post() function. This function takes a data array documented at codex.wordpress.com. When that function is successful it returns the new WordPress Post ID allowing us to use the add_post_meta function to add additional data. The rest of the plug-in code is there to facilitate the WordPress admin menu system and to select and mold the Drupal content into the format WordPress expects. Below are the details.
Data Mapping
Drupal Database Table.Field | WordPress wp_insert_post Data Array Field | Notes |
---|---|---|
node.nid | N/A | This is not stored in WordPress, but is stored in the Drupal NID to Wordpress Post ID helper table. |
N/A | ID | Hard coded to 0 |
node.uid | post_author | I use a helper function to map node.uid to post_author ID and setup the users in WordPress before running the migration. |
node_revisions.title | post_title | |
node_revisions.body | post_content | |
node_revisions.teaser | post_excerpt | |
node_revisions.timestamp | post_date | Formated in Y-m-d |
node.status | post_status | Map Drupal's 1 or 0 bit to "publish" or "draft" |
term_data.name | post_category | I use a helper function to map term_data.name to the correct WordPress Category ID and setup the Categories in WordPress before running the migration. |
N/A | post_type | Hardcoded to "post" for this migration but a helper function of node.type could be used. |
N/A | comment_status | Hardcoded to "closed" |
Standard WordPress Plug-In
The part of the code that makes it show up as a menu item/page in the wordpress admin menu I took straight from the documentation. I added this to the plugins directory as a single file.
The Data Selection and Mapping Code
The helper functions that do the data mappings are straight forward if you are familiar with the Drupal Node and Node Revisions tables.
Full Code Available on GitHub
There is a little more to the code than what I have included here. The entire code snippet can be seen on github at https://gist.github.com/carsonevans/eead315018a0c54f21254325c5b5003d.
I have a newsletter...
Many of my posts end up in Digital Ambit's monthly newsletter. It is the best way to keep up with what Dagny and I are doing in the business world. I appreciate your support and will only send you things we think are valuable.