Skip to content

Instantly share code, notes, and snippets.

@carsonevans
Created December 1, 2016 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carsonevans/eead315018a0c54f21254325c5b5003d to your computer and use it in GitHub Desktop.
Save carsonevans/eead315018a0c54f21254325c5b5003d to your computer and use it in GitHub Desktop.
Old Drupal to WordPress Migration
<?php
/*
Plugin Name: Generic Content Import Tool
Description: One Time Import of Old Drupal Content
*/
// standard wordpress meny hook
add_action( 'admin_menu', 'drupal_import_menu' );
/**
* A standard WordPress plugin menu item
*/
function drupal_import_menu()
{
add_menu_page( "Drupal Import", "Drupal Import", 'import', 'drupal-import', show_drupal_import_menu );
}
/**
* A standard WordPress plugin menu page
* This has an "if is a POST, process the form"
* and then shows the form through normal echo of HTML
*/
function show_drupal_import_menu()
{
if ( !current_user_can( 'import' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
if( isset($_POST[ 'drupal_import_hidden' ]) && $_POST[ 'drupal_import_hidden' ] == 'Y' ) {
$process_limit_value = $_POST['drupal_process_count'];
$limit = 10;
if (is_numeric($process_limit_value))
{
$limit = intval($process_limit_value);
}
$process_number_value = $_POST['drupal_process'];
if (!is_numeric($process_number_value))
{
wp_die( __( 'Invalid process number.' ) );
}
$drupal_db = new wpdb('drupal_user', 'drupal_passwd', 'drupal_database', 'localhost');
if ($process_number_value == 1)
{
echo "<h3>Importing $limit Posts</h3>";
echo '<ul>';
$success_count = 0;
foreach(loadå_data($drupal_db, $limit) as $data)
{
$post_id = wp_insert_post($data, true);
if (is_numeric($post_id))
{
// add_post_meta( $post_id, 'my_custom_meta_field', 'my custom meta data' );
$drupal_db->get_results(sprintf("insert into drupal_import_nid_map (drupal_nid, wordpress_post_id) VALUES (%d, %d)", $data['drupal_nid'], $post_id));
$success_count++;
}
else
{
// print the error to the screen in a very crude way.
echo '<li>' . $data['post_title'] . " :: " . var_export($post_id, true) . "</li>";
}
}
echo '<li> Saved ' . $success_count . " posts successfully</li>";
echo '</ul>';
}
if ($process_number_value == 2)
{
echo "<h3>Importing Categories</h3>";
$sql = "select name from term_data";
$rows = $drupal_db->get_results($sql);
echo "<li>Found " . count($rows) . " categories to process.</li>";
foreach ($rows as $obj) {
wp_create_category($obj->name);
}
}
}
echo '<form method="post" style="padding: 50px">';
echo '<input type="hidden" name="drupal_import_hidden" value="Y" />';
echo '<label for="drupal_process">Process:</label>';
echo '<select name="drupal_process">';
echo '<option value="1">Import Posts</option>';
echo '<option value="2">Add Categories for Drupal</option>';
echo '</select><br />';
echo '<label for="drupal_process_count">Number of Posts to Process:</label>';
echo '<input type="text" name="drupal_process_count" />';
echo '</br>';
echo '<input type="submit" value="Process" />';
echo '</form>';
}
function loadå_data($db, $limit = 10)
{
// get X nodes where X is the passed in limit
$sql = sprintf("select n.nid, n.uid, nr.title, nr.body, nr.teaser, nr.timestamp, n.status from node_revisions nr join node n on n.nid = nr.nid and n.vid = nr.vid and n.type = 'story' left join drupal_import_nid_map m on nr.nid = m.drupal_nid where m.wordpress_post_id is null order by n.created limit %d", $limit);
$rows = $db->get_results($sql);
$data = [];
foreach ($rows as $obj) {
// grab the categories associated with this node and
// store them in an array of categoryIDs
$categories = [];
$get_categories_sql = sprintf("select name from term_data d join term_node n on n.tid = d.tid and n.nid = %d", $obj->nid);
$cats = $db->get_results($get_categories_sql);
foreach ($cats as $catObj) {
$categories[] = get_wp_cat_id($catObj->name);
}
$data[] = [
'ID' => 0,
'post_author' => get_wp_user_id($obj->uid), // maps drupal UID to WP User ID
'post_date' => date('Y-m-d', $obj->timestamp), // Formats Drupal Timestamp to standard date
'post_content' => $obj->body, // HTML
'post_title' => $obj->title,
'post_excerpt' => $obj->teaser,
'post_status' => $obj->status == 1 ? 'publish' : 'draft',
'post_type' => 'post', // I only have one type, but helper function could be used if you have more
'comment_status' => 'closed',
'post_category' => $categories, // see above
'drupal_nid' => $obj->nid // only used by a later function, ignored by WordPress
];
}
return $data;
}
function get_wp_cat_id($drupal_cat_name)
{
switch ($drupal_cat_name) {
case 'Old Category Name':
return 2; // New Category ID
break;
default:
return 1; // uncategorized
break;
}
}
function get_wp_user_id($drupal_uid)
{
switch ($drupal_uid) {
case 301:
return 2; // New User ID
break;
default:
return 1; // Admin
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment