Migrating Bare Bones Yojimbo to WordPress

In a previous post, I wrote about exporting data from Bare Bones Software’s Yojimbo and using Tomboy as an alternative. My migration script scraped the content from Yojimbo Sidekick and wrote XML files in Tomboy Note format. Though there were some drawbacks, such as tags being unavailable in Yojimbo Sidekick, I thought Tomboy’s search feature would be adequate. A couple of weeks trialling Tomboy proved that it wasn’t going to be a Yojimbo killer. Tomboy can’t compete in terms of overall usability and though it’ll work on my Linux and OS X machines, note synchronisation takes some setup that didn’t warrant further time investment.

Once again I turned to WordPress as an easy solution. There’s a risk of seeing WordPress as my hammer for everything that looks like a nail but it has taxonomies, a reliable-enough search functionality and being web-based, works across all my devices. I’m very familiar with the platform and have already built up my own set of tools to export and migrate content. Why not use WordPress? Getting data out of Yojimbo was another issue. The quick and easy Yojimbo Sidekick route already proved inadequate so it was time to dig in and reverse engineer Yojimbo’s storage mechanism.

Analysing and exporting the Yojimbo database

‘Reverse engineering’ turned out to be too lofty a term for the task. It was obvious after quick look that Yojimbo uses an SQLite database to store information. Firing up DB4S to analyse the tables and bit of analysis revealed the tables, columns and relationships that are important for exporting our notes. The columns are a little oddly named but it didn’t take long to figure out the necessary fields for migrating to WordPress.

Table: ZITEM
Column Description
ZBLOB This looks like an ID
ZNAME The Yojimbo note title
Z_PK The ID to the Z_15TAGS relationship table
Table: ZTAG
Column Description
Z_PK The Tag ID
ZNAME The tag name
Table: ZBLOBLSTRINGREP
Column Description
ZBLOB ID
ZSTRING String for unencrypted item
Table: Z_15TAGS
Column Description
Z_15ITEMS1 Relationship ID
Z_25ITAGS Tag ID

Yojimbo SQLite tables

It became a simple matter of tweaking my Drupal to WordPress migration queries to extract from the Yojimbo database and create WordPress posts. Unlike with the Tomboy Notes route, it was possible to recreate the tags, which is what makes Yojimbo so useful. One drawback is that I haven’t figured out how to extract encrypted notes but I didn’t use Yojimbo to store important encrypted information so that wasn’t a priority.

WordPress as a Yojimbo alternative

Using WordPress as a Yojimbo alternative might not work for everyone but after several months use, I’ve found it to be an excellent cross-platform replacement. The installation and database runs on a NAS drive connected to my local network so is accessible to all my devices. Standard WordPress taxonomies, search and plugins makes content management simple once you’ve imported the Yojimbo content. In fact, by migrating away from Yojimo, I’ve ended up creating my own full-blown personal knowledge management system.

If you need to export your Yojimbo notes to a cross-platform alternative, give WordPress a try. You can grab my migration script from GitLab but please keep in mind that it was a quick hack to achieve a specific one-time objective. You may need to hack it to suit your own setup.

Drupal to WordPress migration: user table mapping

This is part four of a series of posts documenting the table mappings for a site migration from Drupal 6 to WordPress 3. For more information, please see the first article in the series.

Table mapping for WordPress users

This maps Drupal user export to WordPress.

Drupal 6.x

WordPress 3.x

Notes

users

wp_posts

uid

ID

name

user_login

Format to lowercase, replace spaces with underscores

pass

user_pass

name

user_nicename

mail

user_email

created

user_registered

Formatted from UNIX time

name

display_name

user_status

Whitespace string

user_activation_key

Set to 0

Table mapping for WordPress user meta values

User information like capabilities and roles in the wp_usermeta table.

users

wp_usermeta

uid

user_id

meta_key

Set to string e.g. ‘wp_capabilities’

meta_value

Set to string e.g. ‘a:1:{s:6:”author”;s:1:”1″;}’

More information about the settings for appropriate meta_key and meta_value can be found in the WordPress Codex:

Node authors and comment authors

Drupal stores both node authors and comment authors in the users table. WordPress handles things differently. Page and post authors are stored in the wp_users table but comment authors are stored in wp_comments together with the comment data.

Drupal to WordPress migration: comments table mapping

This is part three of a series of posts documenting the table mappings for a site migration from Drupal 6 to WordPress 3. For more information, please see the first article in the series.

Table mapping for WordPress comments

Drupal 6.x

WordPress 3.x

Notes

comments

wp_posts

cid

comment_ID

nid

comment_post_ID

timestamp

comment_date

Converted from UNIX timestamp

comment

comment_content

pid

comment_parent

name

comment_author

mail

comment_author_email

homepage

comment_author_url

Truncated to WordPress limit of 200 chars

status

comment_approved

Comment authors

A note about the different ways Drupal and WordPress store comment author information: Drupal stores comment authors in its users table alongside site users like node authors. In WordPress, comment authors are stored in its wp_comments together with the comment data. WordPress comment authors are not entered into the wp_users table.

Drupal to WordPress migration: terms table mapping

This is part two of a series of posts documenting the table mappings for a site migration from Drupal 6 to WordPress 3. For more information, please see the first article in the series.

Table mapping for WordPress terms

This table mapping exports the Drupal terms into WordPress.

Drupal 6.x

WordPress 3.x

Notes

term_data

wp_terms

tid

term_id

name

name

name

slug

Make lower case and convert spaces to underscores

vid

term_group

Not used in a default WordPress installation

term_data

wp_term_taxonomy

tid

term_taxonomy_id

tid

term_id

taxonomy

String: ‘post_tag’ or ‘category’

description

description

parent

0 (No parent)

In the WordPress Taxonomy documentation, “term_group is a means of grouping together similar terms.” During a standard migration, the WordPress term_group is set to the Drupal vocabulary ID, which seems to make sense. Nevertheless, a default WordPress installation does not actually use the value for anything. It may have been included by the developers for future expandability or use by plugins.

term_group=0 is the default value when creating a term using the Drupal user interface.

Below, we associate posts with the newly migrated terms.

Drupal 6.x

WordPress 3.x

term_node

wp_term_relationships

nid

object_id

tid

term_taxonomy_id

Drupal to WordPress migration: posts table mapping

Following on from Drupal to WordPress migration explained, I will create a series of posts documenting the table mappings for a site migration from Drupal 6.x to WordPress 3.x.

To read the mapping, you look up the Drupal table on the left listing the fields we use for a migration. Directly to its right is the WordPress table with the corresponding field in the same row. So for example, the nid in Drupal’s node table is exported to the id field in the WordPress wp_posts table.

I have listed all the fields used in the query. If a Drupal field shows no mapping in the WordPress table, it is being used to match entries in another table for a join. Here we use the vid field in node and node_revisions for an INNER JOIN.

Table mapping for WordPress wp_posts

Drupal 6.x

WordPress 3.x

Notes

node

wp_posts

nid

id

id

post_author

created

post_date

Create date from UNIX timestamp

title

post_title

changed

post_modified

Create date from UNIX timestamp

type

post_type

status

post_status

vid

node_revisions

body

post_content

teaser

post_excerpt

vid

node

url_alias

nid

dst

post_name

If dst field is NULL, use nid

src

to_ping

Whitespace string

pinged

Whitespace string

post_content_filtered

Whitespace string

Query

REPLACE INTO wordpress.wp_posts (
id,
post_author,
post_date,
post_content,
post_title,
post_excerpt,
post_name,
post_modified,
post_type,
post_status,
to_ping,
pinged,
post_content_filtered)
SELECT DISTINCT
n.nid ‘id’,
n.uid ‘post_author’,
DATE_ADD(FROM_UNIXTIME(0), interval n.created second) ‘post_date’,
r.body ‘post_content’,
n.title ‘post_title’,
r.teaser ‘post_excerpt’,
IF(a.dst IS NULL,n.nid, SUBSTRING_INDEX(a.dst, ‘/’, -1)) ‘post_name’,
DATE_ADD(FROM_UNIXTIME(0), interval n.changed second) ‘post_modified’,
n.type ‘post_type’,
IF(n.status = 1, ‘publish’, ‘private’) ‘post_status’,
‘ ‘,
‘ ‘,
‘ ‘
FROM drupal.node n
INNER JOIN drupal.node_revisions r USING(vid)
LEFT OUTER JOIN drupal.url_alias a
ON a.src = CONCAT(‘node/’, n.nid)
WHERE n.type IN (
/* List the content types you want to migrate */
‘page’,
‘story’,
‘blog’,
‘video’,
‘forum’,
‘comment’);

Drupal to WordPress migration SQL queries explained

In this post I will give a step-by-step explanation of my Drupal to WordPress migration SQL queries. For general information about migrating from Drupal to WordPress, please see instead my Drupal to WordPress Migration Guide.

Drupal to WordPress migration queries screenshot

Since I offer site migration as a paid service, readers might be wondering why I’m giving away some of my secret sauce. The simple answer is that the ingredients of the sauce are anything but secret. A web search brings up blog posts and tutorials detailing how to go about it. In fact, the first version of my own Drupal to WordPress migration tool was based on a blog post by another web company.

However, while the knowledge is freely available, the whole process can be a real pain, especially when you’re dealing with a Drupal installation with lots of content and customisations. In my experience, migrating an established site from Drupal to WordPress requires all of the following:

  • An intermediate to advanced level of technical skill;
  • Time to plan, run the migration and do post-migration clean-up;
  • A great deal of patience.

If you are a developer with the right skills, you still need to consider the time investment and effort needed to understand both Drupal and WordPress database schemas. Often, the man-hours required will make a client think twice about proceeding with the project. Fortunately (or unfortunately) for me, I’ve needed to put in the time because my maintenance agreement for some clients covered exactly this sort of work. I’ve therefore built up enough experience to offer competitive quotes for migration of even complex sites. In my very biased opinion, you’ll make better use of your time and budget by getting me to do the task!

Of course, there are still cases where, for whatever reason, it’s not viable to offload the work to someone else. If this sounds like your situation, you’ll probably figure things out eventually so I might as well help you along.

Prerequisites for the Drupal to WordPress migration script

To run this migration you will need:

  • A working installation of Drupal 6;
  • A clean installation of WordPress 3.5 or above;
  • Access to the Drupal and WordPress MySQL databases;
  • The ability to run SQL queries on both databases;
  • Both databases on the same MySQL server;
  • Access to the Drupal and WordPress installations.

Ideally, you should:

  • Run the migration from a development server (it’s best not to risk running this on your live server);
  • Backup your live Drupal database before beginning the migration;
  • Be comfortable with running database operations;
  • Have planned the migration beforehand (e.g. which content types and taxonomies should be migrated).

If you have an older version of Drupal, simply upgrade to Drupal 6 first, then run the migration. I mention WordPress 3.5 as a prerequisite because that’s where I’ve done most testing. You can probably get away with migrating straight into a more recent version but to avoid any problems, I suggest you start off with WordPress 3.5. It’s easy to upgrade to the latest WordPress version after converting the Drupal content.

The Drupal to WordPress migration SQL queries

Keep in mind that this article is based on my Drupal to WordPress migration tool but with some additional queries written for the specific needs of a client. It therefore includes some values which will not apply to your site. I’ve stripped out any identifying information but left generic data to provide an example. You will need to manually look up the correct values in the Drupal database for your installation.

Another thing to keep in mind is that I’ve favoured readability over efficiency. For example, it’s possible to write more complex queries to avoid creating working tables but that would make debugging more difficult. Having a trail of data changes can help with content analysis if the end results aren’t what you expected.

CAUTION: Make a backup of both your Drupal and WordPress databases before running these queries. USE IS ENTIRELY AT YOUR OWN RISK. I’m offering this information with no warranty or support implied.

Example data in this scenario

  • wordpress: the WordPress database name.
  • drupal: the Drupal database name.
  • acc_ table prefixes: these are working tables I create to help with migrating data.

Clear out some Drupal and WordPress tables

For most migration projects to date, I’ve needed to run through several passes of the queries as I make incremental adjustments based on the client’s feedback. I define a ‘pass’ as one iteration of the entire migration process and inspecting the results in a WordPress installation. Depending on your project requirements, you may need to do this several times, making little tweaks to the MySQL queries as you go along.

These Drupal and WordPress tables may be empty or non-existent if you’re running the queries for the first time but it makes sense to clear them out at the start.

TRUNCATE TABLE wordpress.wp_comments;
TRUNCATE TABLE wordpress.wp_links;
TRUNCATE TABLE wordpress.wp_postmeta;
TRUNCATE TABLE wordpress.wp_posts;
TRUNCATE TABLE wordpress.wp_term_relationships;
TRUNCATE TABLE wordpress.wp_term_taxonomy;
TRUNCATE TABLE wordpress.wp_terms;
TRUNCATE TABLE wordpress.wp_users;

DROP TABLE IF EXISTS drupal.acc_duplicates;
DROP TABLE IF EXISTS drupal.acc_news_terms;
DROP TABLE IF EXISTS drupal.acc_tags_terms;
DROP TABLE IF EXISTS drupal.acc_wp_tags;
DROP TABLE IF EXISTS drupal.acc_users_post_count;
DROP TABLE IF EXISTS drupal.acc_users_comment_count;
DROP TABLE IF EXISTS drupal.acc_users_with_content;
DROP TABLE IF EXISTS drupal.acc_users_post_count;

For some installations, I make changes to the wp_usermeta table so that needs to be cleared too.

TRUNCATE TABLE wordpress.wp_usermeta;

Vocabularies and taxonomies

Delete unwanted vocabularies. You’ll need to look in your Drupal vocabulary table for the appropriate vids. In this case, I’m deleting vocabularies 5, 7, 8, 38 and 40.

DELETE FROM drupal.vocabulary WHERE vid IN (5, 7, 8, 38, 40);

Delete terms associated with unwanted vocabularies. Here I’m keeping the terms for vid 38. Sometimes you might want to keep some terms of unwanted vocabularies for later conversion into into WordPress tags. (Please see the next query.)

DELETE FROM drupal.term_data WHERE vid IN (5, 7, 8, 40);

You may want to merge terms. In this example, I am merging the previously saved terms for News which has vid 38 to the Tags vocabulary terms which has vid 2.

We will need to deal with duplicates. For example, in the Drupal installation, ‘science’ could appear in both News (vid 38) and Tags (vid 2). This will cause a problem when exporting to WordPress since we can’t have duplicate terms. Here I create working tables for both term groups.

CREATE TABLE drupal.acc_news_terms AS SELECT tid, vid, name FROM drupal.term_data WHERE vid=38;
CREATE TABLE drupal.acc_tags_terms AS SELECT tid, vid, name FROM drupal.term_data WHERE vid=2;

Create a working table from duplicates.

CREATE TABLE drupal.acc_duplicates AS
SELECT t.tid tag_tid,
n.tid news_tid,
t.vid tag_vid,
n.vid news_vid,
t.name
FROM drupal.acc_tags_terms AS t
INNER JOIN (drupal.acc_news_terms AS n)
ON n.name=t.name;

Append a string to News terms duplicates so they won’t clash during migration. Here I used a fixed string but this won’t work if you have more than two terms with the same name. If you expect many terms with the same name, it would be better to generate a unique number. For example, using the tid would make it unique since these are unique primary keys. Use whatever string makes sense for your project.

Note that we’re overwriting the source data in the Drupal term_data table so proceed with care. Make sure you have a backup of your pre-migration Drupal tables in case you need to run the conversion again.

UPDATE drupal.term_data
SET name=CONCAT(name, ‘_01’)
WHERE tid IN (SELECT news_tid FROM drupal.acc_duplicates);

Convert Drupal News terms to Drupal Tags. We’ll migrate the whole lot into WordPress tags later.

UPDATE drupal.term_data SET vid=2 WHERE vid=38;

Create a table of WordPress tags. Exclude any terms from Drupal vocabularies that you might later migrate into WordPress categories. See the MySQL queries below where I create WordPress categories and sub-categories.

Here, all Drupal vocabularies except 37, 36 and 35 will be converted WordPress tags.

CREATE TABLE drupal.acc_wp_tags AS
SELECT
tid,
vid,
name
FROM drupal.term_data
WHERE vid NOT IN (37, 36, 35);

Now create the tags in the WordPress database. A clean WordPress database will have term_id=1 for ‘Uncategorized’. Use REPLACE as this may conflict with a Drupal tid.

We are assuming that this point the Drupal term_data table has been cleaned of any duplicate names. Any duplicate terms will be lost when running this MySQL query.

REPLACE INTO wordpress.wp_terms (term_id, name, slug, term_group)
SELECT
d.tid,
d.name,
REPLACE(LOWER(d.name), ‘ ‘, ‘_’),
d.vid
FROM drupal.term_data d WHERE d.tid IN (
SELECT t.tid FROM drupal.acc_wp_tags t
);

In WordPress, tags and categories are all stored in the wp_term_taxonomy table. The taxonomy field specifies whether it’s a tag or category by setting the field string to either ‘post_tag’ or ‘category’.

Here I convert these Drupal terms into WordPress tags.

REPLACE INTO wordpress.wp_term_taxonomy (
term_taxonomy_id,
term_id,
taxonomy,
description,
parent)
SELECT DISTINCT
d.tid,
d.tid ‘term_id’,
‘post_tag’, /* This string makes them WordPress tags */
d.description ‘description’,
0 /* In this case, I don’t give tags a parent */
FROM drupal.term_data d
WHERE d.tid IN (SELECT t.tid FROM drupal.acc_wp_tags t);

Create the categories and sub-categories in the WordPress database. This may be unnecessary depending on your setup.

Add terms associated with a Drupal vocabulary into WordPress. Note that in this case, these are the same vids that I excluded from the tag table above.

REPLACE INTO wordpress.wp_terms (term_id, name, slug, term_group)
SELECT DISTINCT
d.tid,
d.name,
REPLACE(LOWER(d.name), ‘ ‘, ‘_’),
d.vid
FROM drupal.term_data d
WHERE d.vid IN (37, 36, 35);

Convert these Drupal terms into WordPress sub-categories by setting the parent field in the wp_term_taxonomy table.

REPLACE INTO wordpress.wp_term_taxonomy (
term_taxonomy_id,
term_id,
taxonomy,
description,
parent)
SELECT DISTINCT
d.tid,
d.tid ‘term_id’,
‘category’,
d.description ‘description’,
d.vid
FROM drupal.term_data d
WHERE d.vid IN (37, 36, 35);

Now add the vocabularies to the WordPress terms table. There’s no need to set term_id as vocabularies are not directly associated with posts.

INSERT INTO wordpress.wp_terms (name, slug, term_group)
SELECT DISTINCT
v.name,
REPLACE(LOWER(v.name), ‘ ‘, ‘_’),
v.vid
FROM drupal.vocabulary v
WHERE vid IN (37, 36, 35);

Insert Drupal vocabularies as WordPress categories.

INSERT INTO wordpress.wp_term_taxonomy (
term_id,
taxonomy,
description,
parent,
count)
SELECT DISTINCT
v.vid,
‘category’, /* This string makes them WordPress categories */
v.description,
v.vid,
0
FROM drupal.vocabulary v
WHERE vid IN (37, 36, 35);

Update the WordPress term groups and parents.

Before continuing with this step, we need to manually inspect the table to get the term_id for the parents inserted above. In this case, vids 37, 36 and 35 were inserted as into the wp_term_taxonomy table as term_ids 7517, 7518 and 7519. I will use them as the parents for their respective terms. In other words, terms that formerly belonged to the Drupal vocabulary ID 37 would now belong to the WordPress parent category 7519.

UPDATE wordpress.wp_terms SET term_group=7519 WHERE term_group=37;
UPDATE wordpress.wp_terms SET term_group=7518 WHERE term_group=36;
UPDATE wordpress.wp_terms SET term_group=7517 WHERE term_group=35;

UPDATE wordpress.wp_term_taxonomy SET parent=7519 WHERE parent=37;
UPDATE wordpress.wp_term_taxonomy SET parent=7518 WHERE parent=36;
UPDATE wordpress.wp_term_taxonomy SET parent=7517 WHERE parent=35;

UPDATE wordpress.wp_term_taxonomy SET term_id=7519 WHERE term_taxonomy_id=7519;
UPDATE wordpress.wp_term_taxonomy SET term_id=7518 WHERE term_taxonomy_id=7518;
UPDATE wordpress.wp_term_taxonomy SET term_id=7517 WHERE term_taxonomy_id=7517;

Re-insert the Uncategorized term replaced earlier in the conversion process. We may have replaced or deleted the Uncategorized category during a previous MySQL query. Re-insert it if you want an Uncategorized category.

INSERT INTO wordpress.wp_terms (name, slug, term_group)
VALUES (‘Uncategorized’, ‘uncategorized’, 0);
INSERT INTO wordpress.wp_term_taxonomy (
term_taxonomy_id,
term_id,
taxonomy,
description,
parent,
count)
SELECT DISTINCT
t.term_id,
t.term_id,
‘category’,
t.name,
0,
0
FROM wordpress.wp_terms t
WHERE t.slug=’uncategorized’;

Converting Drupal nodes to WordPress posts

Now create WordPress posts from Drupal nodes. This may take a while if you have many Drupal nodes. Wait until the query completes before continuing. It could take several minutes.

REPLACE INTO wordpress.wp_posts (
id,
post_author,
post_date,
post_content,
post_title,
post_excerpt,
post_name,
post_modified,
post_type,
post_status,
to_ping,
pinged,
post_content_filtered)
SELECT DISTINCT
n.nid ‘id’,
n.uid ‘post_author’,
DATE_ADD(FROM_UNIXTIME(0), interval n.created second) ‘post_date’,
r.body ‘post_content’,
n.title ‘post_title’,
r.teaser ‘post_excerpt’,
IF(a.dst IS NULL,n.nid, SUBSTRING_INDEX(a.dst, ‘/’, -1)) ‘post_name’,
DATE_ADD(FROM_UNIXTIME(0), interval n.changed second) ‘post_modified’,
n.type ‘post_type’,
IF(n.status = 1, ‘publish’, ‘private’) ‘post_status’,
‘ ‘,
‘ ‘,
‘ ‘
FROM drupal.node n
INNER JOIN drupal.node_revisions r USING(vid)
LEFT OUTER JOIN drupal.url_alias a
ON a.src = CONCAT(‘node/’, n.nid)
WHERE n.type IN (
/* List the content types you want to migrate */
‘page’,
‘story’,
‘blog’,
‘video’,
‘forum’,
‘comment’);

Set the Drupal content types that should be migrated as WordPress ‘posts’. In this case, I want ‘page’, ‘story’, ‘blog’, ‘video’, ‘forum’ and ‘comment’ in Drupal to be converted to posts in WordPress.

UPDATE wordpress.wp_posts SET post_type = ‘post’
WHERE post_type IN (
‘page’,
‘story’,
‘blog’,
‘video’,
‘forum’,
‘comment’);

Now convert the remaining content types into WordPress pages.

UPDATE wordpress.wp_posts SET post_type = ‘page’ WHERE post_type NOT IN (‘post’);

Housekeeping queries for terms

Here I associate the content with WordPress terms using the wp_term_relationships table.

INSERT INTO wordpress.wp_term_relationships (
object_id,
term_taxonomy_id)
SELECT DISTINCT nid, tid FROM drupal.term_node;

We need to update tag counts.

UPDATE wordpress.wp_term_taxonomy tt
SET count = ( SELECT COUNT(tr.object_id)
FROM wordpress.wp_term_relationships tr
WHERE tr.term_taxonomy_id = tt.term_taxonomy_id);

Now set the default WordPress category. You’ll need to manually look in the database for the term_id of the category you want to set as the default WordPress category.

UPDATE wordpress.wp_options SET option_value=’7520′ WHERE option_name=’default_category’;
UPDATE wordpress.wp_term_taxonomy SET taxonomy=’category’ WHERE term_id=7520;

Migrate comments

REPLACE INTO wordpress.wp_comments (
comment_ID,
comment_post_ID,
comment_date,
comment_content,
comment_parent,
comment_author,
comment_author_email,
comment_author_url,
comment_approved)
SELECT DISTINCT
cid,
nid,
FROM_UNIXTIME(timestamp),
comment,
pid,
name,
mail,
SUBSTRING(homepage,1,200),
((status + 1) % 2) FROM drupal.comments;

Update comment counts.

UPDATE wordpress.wp_posts
SET comment_count = ( SELECT COUNT(comment_post_id)
FROM wordpress.wp_comments
WHERE wordpress.wp_posts.id = wordpress.wp_comments.comment_post_id);

Migrate Drupal Authors into WordPress

In this case I am migrating only users who have created a post. This was a requirement for my project but may be unnecessary for you.

First delete all existing WordPress authors except for admin.

DELETE FROM wordpress.wp_users WHERE ID > 1;
DELETE FROM wordpress.wp_usermeta WHERE user_id > 1;

Now set Drupal’s admin password to a known value. This avoids hassles with trying to reset the password on the new WordPress installation. Resetting a user password in WordPress is more convoluted and cannot be done using a simple MySQL query as in Drupal.

UPDATE drupal.users set pass=md5(‘password’) where uid = 1;

Create a working table of users and the number of posts they’ve authored. I am only considering authors who have created posts of the content types I want to migrate.

CREATE TABLE drupal.acc_users_post_count AS
SELECT
u.uid,
u.name,
u.mail,
count(n.uid) node_count
FROM drupal.node n
INNER JOIN drupal.users u on n.uid = u.uid
WHERE n.type IN (
/* List the post types I migrated earlier */
‘page’,
‘story’,
‘blog’,
‘video’,
‘forum’,
‘comment’)
GROUP BY u.uid
ORDER BY node_count;

Now add these authors into the WordPress wp_users table.

INSERT IGNORE INTO wordpress.wp_users (
ID,
user_login,
user_pass,
user_nicename,
user_email,
user_registered,
user_activation_key,
user_status,
display_name)
SELECT DISTINCT
u.uid,
REPLACE(LOWER(u.name), ‘ ‘, ‘_’),
u.pass,
u.name,
u.mail,
FROM_UNIXTIME(created),
”,
0,
u.name
FROM drupal.users u
WHERE u.uid IN (SELECT uid FROM drupal.acc_users_post_count);

First set all these authors to WordPress “author” by default. In the next MySQL query, we can selectively promote individual authors to other WordPress roles.

INSERT IGNORE INTO wordpress.wp_usermeta (
user_id,
meta_key,
meta_value)
SELECT DISTINCT
u.uid,
‘wp_capabilities’,
‘a:1:{s:6:”author”;s:1:”1″;}’
FROM drupal.users u
WHERE u.uid IN (SELECT uid FROM drupal.acc_users_post_count);

INSERT IGNORE INTO wordpress.wp_usermeta (
user_id,
meta_key,
meta_value)
SELECT DISTINCT
u.uid,
‘wp_user_level’,
‘2’
FROM drupal.users u
WHERE u.uid IN (SELECT uid FROM drupal.acc_users_post_count);

During the course of the migration, some posts may end up not having an assigned author. Here I reassign authorship for these posts to the WordPress admin user.

UPDATE wordpress.wp_posts
SET post_author = 1
WHERE post_author NOT IN (SELECT DISTINCT ID FROM wordpress.wp_users);

Comment authors

In Drupal, comments are treated as nodes and comment authors are stored along with other node authors in the Drupal users table. WordPress treats comments and comment authors differently. Comment authors in WordPress are not stored in the wp_users table. Instead, they’re stored along with the comment content itself in the wp_comments table.

We may need to run additional query to import users who have commented but haven’t created any of the selected content types. To do this, I create some working tables required for some later MySQL queries:

  • acc_users_with_comments: empty copy of wp_users
  • acc_users_add_commenters: empty copy of wp_users
  • acc_wp_users: copy of wp_users from wordpress database containing users

Running the following MySQL queries will throw errors if you haven’t created the required tables above.

First create a working table of Drupal users who have created a Drupal comment.

CREATE TABLE drupal.acc_users_comment_count AS
SELECT
u.uid,
u.name,
count(c.uid) comment_count
FROM drupal.comments c
INNER JOIN drupal.users u on c.uid = u.uid
GROUP BY u.uid;

Now add the author information for these users into another working table.

INSERT IGNORE INTO drupal.acc_users_with_comments (
ID,
user_login,
user_pass,
user_nicename,
user_email,
user_registered,
user_activation_key,
user_status,
display_name)
SELECT DISTINCT
u.uid,
REPLACE(LOWER(u.name), ‘ ‘, ‘_’),
u.pass,
u.name,
u.mail,
FROM_UNIXTIME(created),
”,
0,
u.name
FROM drupal.users u
WHERE u.uid IN (SELECT uid FROM drupal.acc_users_comment_count);

Using the above table, next build a working table of Drupal users who have commented but have not already been added to the WordPress wp_users table.

INSERT IGNORE INTO drupal.acc_users_add_commenters (
ID,
user_login,
user_pass,
user_nicename,
user_email,
user_registered,
user_activation_key,
user_status,
display_name)
SELECT DISTINCT
u.ID,
u.user_login,
u.user_pass,
u.user_nicename,
u.user_email,
u.user_registered,
”,
0,
u.display_name
FROM drupal.acc_users_with_comments u
WHERE u.ID NOT IN (SELECT ID FROM drupal.wp_users);

Combine the tables into another working table acc_wp_users.

INSERT IGNORE
INTO drupal.acc_wp_users
SELECT *
FROM drupal.acc_users_add_commenters;

The acc_wp_users working table helps when inspecting the user list. For example, you might want to clear out inactive users or spam posters. Once finished, remember to replace your WordPress wp_users with the cleaned acc_wp_users table. You may prefer to amend the above query to insert directly into the WordPress wp_users table.

I realise this is a rather round-about way of migrating comment authors from Drupal into WordPress but find that having working tables helps with debugging.

Housekeeping for WordPress options

Update file path for the WordPress installation.

UPDATE wordpress.wp_posts SET post_content = REPLACE(post_content, ‘”/files/’, ‘”/wp-content/uploads/’);

Set your WordPress site name using the Drupal ‘site_name’ variable.

UPDATE wordpress.wp_options SET option_value = ( SELECT value FROM drupal.variable WHERE name=’site_name’) WHERE option_name = ‘blogname’;

Set your WordPress site description using the Drupal ‘site_slogan’ variable.

UPDATE wordpress.wp_options SET option_value = ( SELECT value FROM drupal.variable WHERE name=’site_slogan’) WHERE option_name = ‘blogdescription’;

Set the WordPress site email address.

UPDATE wordpress.wp_options SET option_value = ( SELECT value FROM drupal.variable WHERE name=’site_mail’) WHERE option_name = ‘admin_email’;

Set the WordPress permalink structure. Here we’re using /%postname%/ but you may set it according your own needs.

UPDATE wordpress.wp_options SET option_value = ‘/%postname%/’ WHERE option_name = ‘permalink_structure’;

Create URL redirects table

This table will not be used for the migration but may be useful if you need to manually create redirects from Drupal aliases. You will need the entries here for search engine optimisation (SEO) of your new WordPress site.

DROP TABLE IF EXISTS drupal.acc_redirects;
CREATE TABLE drupal.acc_redirects AS
SELECT
CONCAT(‘drupal/’,
IF(a.dst IS NULL,
CONCAT(‘node/’, n.nid),
a.dst
)
) ‘old_url’,
IF(a.dst IS NULL,n.nid, SUBSTRING_INDEX(a.dst, ‘/’, -1)) ‘new_url’,
‘301’ redirect_code
FROM drupal.node n
INNER JOIN drupal.node_revisions r USING(vid)
LEFT OUTER JOIN drupal.url_alias a
ON a.src = CONCAT(‘node/’, n.nid)
WHERE n.type IN (
/* List the post types I migrated earlier */
‘page’,
‘story’,
‘blog’,
‘video’,
‘forum’,
‘comment’);

Finalising the conversion

Now that we’ve finished converting the content over from Drupal to WordPress, we have the rather (not very) fun job of checking the content, setting up any WordPress plugins and widgets, then finally going live. Depending on the complexity of your Drupal installation, this process can be extremely time-consuming and perhaps form a separate project in its own right.

You can use my Drupal to WordPress migration notes to help with going live. Of course, the search for equivalent WordPress plugins and conversion from the old Drupal modules will have to be done according to your specific set-up.

Accepting limitations

Before finishing this article, one point I’d like to convey is the importance of accepting the limitations of any migration process. You, or your client, may be insistent on turning your new WordPress site into an exact copy of your former Drupal installation. While this may be technically possible, there comes a point of diminishing returns where the work you’d need to put in just isn’t worth the value of the data.

It may also be more productive in some instances to make manual adjustments via the WordPress control panel than to try anything clever using the backend database. Sometimes the time needed to write, test and debug MySQL queries far exceeds the boring but more reliable editing using the web-front end.

Expect to kill your SEO if you’re not careful with the migration. A site that relies heavily on revenue from search engine rankings will need extra steps to preserve SEO. This is a huge topic so I will not cover it here but you should carefully plan the conversion steps before starting with the migration. Pay particular attention to preserving Drupal path aliases, taxonomy listing pages and internal links.

Good luck!

So that’s it. A Drupal to WordPress migration can be a great deal of effort. I’ve had one project that initially looked like only couple of hours work balloon to over 50 billable hours in total. Previously installed modules caused problems requiring custom MySQL queries and PHP scripting to resolve. On the other hand, I’ve had a number of sites that completed in 15 minutes after running my Drupal to WordPress migration tool.

Overall, the majority of my clients prefer WordPress over their previous Drupal site. I personally find WordPress quicker to update and manage. Any short-term hassles with migrating have been outweighed by the long-term advantages of easier maintenance.

Getting the code and submitting improvements

You can find the code on my GitHub repository. The queries in this article can be found in the file drupaltowordpress-custom.sql.

I’d love to receive corrections, bug fixes and suggestions for improvements. Please contact me or submit an issue on GitHub.

CAUTION: Make a backup of both your Drupal and WordPress databases before running these queries. USE IS ENTIRELY AT YOUR OWN RISK. I’m offering this information with no warranty or support implied.

Drupal to WordPress migration notes

These Drupal to WordPress migration notes are intended for clients who are handling some aspects of the migration themselves. Users of the Drupal to WordPress Migration Tool or MySQL queries might also find information here to resolve some problems.

Admin account password and email address

Your content management system (CMS) administrator password and email address may have changed during the migration to help with debugging. Please update them as soon as possible.

Drupal and WordPress user passwords are encrypted and I do not have access to them in plaintext. However, for your peace of mind, I recommend that you also ask all your users to reset their passwords.

Server credentials

Please remember to change any database, (S)FTP, SSH server and control panel credentials you may have given me.

Working tables

During the migration process, I may create some working tables to hold temporary data. My naming convention for the working tables are as follows:

  • acc_ prefix: these are working tables created to help with migrating data.
  • backup_ prefix: these are backups of existing tables prior to migration, for example from a previous installation of your CMS. These will not be altered during the migration process.
  • _copy postfix: duplicates of WordPress tables copied for debugging will have a _copy postfix. These are different from the backup_ tables in that they may have been manually altered.

These working tables are not used by WordPress so you may safely delete them. However, it’s advisable to keep them for a short while as they may be useful in case we need to debug any issues that crop up.

Migrating to a live server

Most Drupal to WordPress migrations are performed on a test or development server. For help on how to move WordPress to your live server, please see: WordPress Codex: Moving WordPress.

Troubleshooting common errors after a site migration

Please see below for some common problems you may experience after migrating your Drupal content to a new WordPress site.

Incorrect domain in URLs

WordPress stores domains in the database. If you performed the migration on a local or development server, there’s a good chance that the links will be incorrect after migrating to your live server. Use the Interconnect IT utility to run a search and replace on your database. This will also correct changed database prefixes.

More information can be found on the interconnect/it Search Replace DB page.

“Sorry, you are not allowed to access this page”

If you try to log in via wp-admin and receive this error, you may have overwritten the administrator user’s wp_capabilities or wp_user_level.

Set the correct meta_value using the following SQL:

UPDATE wp_usermeta 
    SET meta_value = 'a:1:{s:13:"administrator";s:1:"1";}'
    WHERE user_id = 1 AND meta_key = 'wp_capabilities';

UPDATE wp_usermeta 
    SET meta_value = '10'
    WHERE user_id  = 1 AND meta_key = 'wp_user_level';

Remember to use the appropriate the user_id.

“You do not have sufficient permissions to access this page”

If you receive this error after logging in to your new WordPress installation, it’s possible that the database prefix on your new WordPress site is not set correctly. This may happen if you move your WordPress installation to a host that uses a different database prefix.

Try running one of the queries below. Replace wp_new_usermeta, oldprefix_ and newprefix_ as appropriate.

Option 1:

UPDATE wp_new_usermeta SET meta_key = REPLACE(meta_key,’oldprefix_’,’newprefix_’);

UPDATE wp_new_options SET option_name = REPLACE(option_name,’oldprefix_’,’newprefix_’);

Option 2:

update wp_new_usermeta set meta_key = ‘newprefix_usermeta’ where meta_key = ‘wp_capabilities’;

update wp_new_usermeta set meta_key = ‘newprefix_user_level’ where meta_key = ‘wp_user_level’;

update wp_new_usermeta set meta_key = ‘newprefix_autosave_draft_ids’ where meta_key = ‘wp_autosave_draft_ids’;

update wp_new_options set option_name = ‘newprefix_user_roles’ where option_name = ‘wp_user_roles’;

Please note that these queries may not work for you. Success depends on your specific setup.

For more information, please see the following pages:

“Unable to establish database connection”

The database credentials are correct but you see an “Unable to establish database connection” error. Check that wp_options table is not empty.

Allowed memory size exhausted

  1. Increase php.ini memory size
  2. Increase WP settings memory limit with:
    define(‘WP_MEMORY_LIMIT’, ‘128M’);

Post migrated but navigating to post shows blank page

The post is visible in the dashboard but viewing it displays a blank (but themed) page. Manually saving it on the dashboard makes it appear.

It’s possible that the problem posts need to be assigned to a category.

Post migrated but navigating to post shows ‘Not found’ error

The post is visible in the dashboard but viewing it displays a ‘not found’ error. Manually saving it on the dashboard makes it appear.

  1. Check that the url contains valid characters for a WordPress slug.
  2. Check that .htaccess is enabled in your Apache configuration

Link to author 404

Posts exist but link to the author’s post listing page is broken.

Check user_nicename in wp_users WordPress table. Make sure the nicename doesn’t contain invalid characters such as spaces, periods.

Dashboard controls not visible after logging in

If you are able to log in as an administrator user but do not see the Dashboard controls, check your table prefixes. If the table prefixes changed during the migration, you may have forgotten to update the options and usermeta tables.

Check where the old prefixes have been set:

SELECT * FROM `wpnew_options` WHERE `option_name` LIKE '%wp_%';
SELECT * FROM `wpnew_usermeta` WHERE `meta_key` LIKE '%wp_%';

Updated the prefixes. For example:

UPDATE `wpnew_options` SET `option_name` = 'wpnew_user_roles' WHERE `option_name` = 'wp_user_roles';
UPDATE `wpnew_usermeta` SET `meta_key` = 'wpnew_capabilities' WHERE `meta_key` = 'wp_capabilities';
UPDATE `wpnew_usermeta` SET `meta_key` = 'wpnew_user_level' WHERE `meta_key` = 'wp_user_level';

UPDATE `wpnew_usermeta` SET `meta_key` = 'wpnew_user-settings-time' WHERE `meta_key` = 'wp_user-settings-time';
UPDATE `wpnew_usermeta` SET `meta_key` = 'wpnew_user-settings' WHERE `meta_key` = 'wp_user-settings';

Further help

We’ll be very happy to provide support you if have difficulties after migration. For a quotation, please contact us. We also offer customised hosting and maintenance packages. Please ask for details.