Our Blog

A guide for migrating Drupal to WordPress in a few simple steps

migrating Drupal to Wordpress

Drupal is one of the most efficient content management systems (CMS) available for web development but it can be hard to learn and therefore WordPress scores over it as the open source platform is extremely easy to understand and use. The conveniences offered on the platform prompt many users to look for services helping in migrating Drupal to WordPress and a step by step guide is being presented here that will help any person looking to convert a website from Drupal to WordPress theme.

The process being described here can be undertaken by a technically proficient person with knowledge of code and SQL queries. Moreover, it is advised that before starting the procedure, backups of both databases must be made and also check whether the categories and tags in the original Drupal install are labeled correctly.

Steps for Converting From Drupal To WordPress

A step by step solution for users looking to convert Drupal to WordPress is being presented here :

Step 1 :

Create a new WordPress website and clear all the previous content with the following command in the SQL queries tab of the database in phpmyadmin :

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;

Step 2 : Multiple users can be converted over with the following code :

DELETE FROM wordpress.wp_users WHERE ID > 1;

DELETE FROM wordpress.wp_usermeta WHERE user_id > 1;

Step 3 :

Tags can be migrated using this code but check that all duplicate names in the Drupal term_data table have been removed to ensure they are not lost :

REPLACE INTO wordpress.wp_terms
(term_id, `name`, slug, term_group)
SELECT DISTINCT
d.tid, d.name, REPLACE(LOWER(d.name), ‘ ‘, ‘_’), 0
FROM drupal.term_data d
INNER JOIN drupal.term_hierarchy h
USING(tid)
WHERE (1
)
;
INSERT INTO wordpress.wp_term_taxonomy
(term_id, taxonomy, description, parent)
SELECT DISTINCT
d.tid `term_id`,
‘post_tag’ `taxonomy`,
d.description `description`,
h.parent `parent`
FROM drupal.term_data d
INNER JOIN drupal.term_hierarchy h
USING(tid)
INNER JOIN drupal.term_node n
USING(tid)
WHERE (1
)
;

Step 4 : Posts can be migrated through the application of the following query :

INSERT INTO wordpress.wp_posts
(id, post_author, post_date, post_content, post_title, post_excerpt,
post_name, post_modified, post_type, `post_status`)
SELECT DISTINCT
n.nid `id`,
n.uid `post_author`,
FROM_UNIXTIME(n.created) `post_date`,
r.body `post_content`,
n.title `post_title`,
r.teaser `post_excerpt`,
IF(SUBSTR(a.dst, 11, 1) = ‘/’, SUBSTR(a.dst, 12), a.dst) `post_name`,
FROM_UNIXTIME(n.changed) `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)
# Add more Drupal content types below if applicable.
WHERE n.type IN (‘post’, ‘page’, ‘blog’)
;

Please note that if the Drupal installation has more than one post types, then add the name of the post type in the following line as not doing so will result in a failure in migrating all post types to WordPress :

WHERE n.type IN (‘post’, ‘page’, ‘blog’)

Step 5 : Any user desiring to combine post types in WordPress can use this code :

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

Step 6 : The post/ tag relationship can be defined by these lines of code :

INSERT INTO wordpress.wp_term_relationships (object_id, term_taxonomy_id)
SELECT DISTINCT nid, tid FROM drupal.term_node
;
# Update tag counts.
UPDATE wp_term_taxonomy tt
SET `count` = (
SELECT COUNT(tr.object_id)
FROM wp_term_relationships tr
WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
)
;

Step 7 : Comments can be migrated by using this query :

INSERT INTO wordpress.wp_comments
(comment_post_ID, comment_date, comment_content,
comment_parent, comment_author,
comment_author_email, comment_author_url, comment_
approved)
SELECT DISTINCT
nid, FROM_UNIXTIME(timestamp), comment, thread, name,
mail, homepage, ((status + 1) % 2)
FROM drupal.comments
;
# Update comments count on wp_posts table.
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`
)
;

Step 8 :

No action is required to be taken if the user does not want to change the location where the Drupal images and files are stored currently but if they are being moved through FTP to the uploads folder of the WordPress admin, then the following line of code is needed for fixing the URLs of the images :

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

Step 9 : Taxonomy can be fixed with these lines :

UPDATE IGNORE wordpress.wp_term_relationships, wordpress.wp_term_taxonomy
SET wordpress.wp_term_relationships.term_taxonomy_id = wordpress.wp_term_taxonomy.term_taxonomy_id
WHERE wordpress.wp_term_relationships.term_taxonomy_id = wordpress.wp_term_taxonomy.term_id
;

Step 10 : Users can be assigned author roles with this code :

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, u.mail, NULL, u.name, u.mail,
FROM_UNIXTIME(created), ”, 0, u.name
FROM drupal.users u
INNER JOIN drupal.users_roles r
USING (uid)
WHERE (1
# Uncomment and enter any email addresses you want to exclude below.
# AND u.mail NOT IN (‘test@example.com’)
)
;

Step 11 : This code will help in assigning authors to the posts written by them :

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

Step 12  : Author roles permissions can be set with the help of these lines of code :

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
INNER JOIN drupal.users_roles r
USING (uid)
WHERE (1
# Uncomment and enter any email addresses you want to exclude below.
# AND u.mail NOT IN (‘test@example.com’)
)
;
INSERT IGNORE INTO wordpress.wp_usermeta (user_id, meta_key, meta_value)
SELECT DISTINCT
u.uid, ‘wp_user_level’, ‘2’
FROM drupal.users u
INNER JOIN drupal.users_roles r
USING (uid)
WHERE (1
# Uncomment and enter any email addresses you want to exclude below.
# AND u.mail NOT IN (‘test@example.com’)
)
;

Step 13:

The next in migrating Drupal to WordPress involves assigning of administrator status which is made possible with the code given below :

UPDATE wordpress.wp_usermeta
SET meta_value = ‘a:1:{s:13:”administrator”;s:1:”1″;}’
WHERE user_id IN (1) AND meta_key = ‘wp_capabilities’
;
UPDATE wordpress.wp_usermeta
SET meta_value = ’10’
WHERE user_id IN (1) AND meta_key = ‘wp_user_level’
;

Step 14 :

Finally, entering these lines in the editor will help in ensuring that the posts do not look strange after conversion :

UPDATE wordpress.wp_posts
SET post_name =
REVERSE(SUBSTRING(REVERSE(post_name),1,LOCATE(‘/’,REVERSE(post_name))-1))
;

The procedure of conversion is complete but some problems may occur such as duplicate errors when entering SQL through conversion and the solution lies in checking for the last known table that was converted and converting only from after that table. Another common problem that occurs is that individual posts and page links redirect a visitor to the home page which means that the slugs need to be updated and a solution for the problem is presented here :

// We prepare a variable to hold arrays with post titles so we don’t accidentally make duplicates, that would be bad
$slug_done = array();
// Run a query to grab all the posts, we only want posts/pages though
$posts = $wpdb->get_results( ”
SELECT
`ID`,
`post_title`
FROM
`” . $wpdb->posts . “`
WHERE
`post_type` = ‘page’
OR
`post_type` = ‘post’
” );
// Loop through results
foreach( $posts AS $single )
{
// Generate a URL friendly slug from the title
$slug_base = sanitize_title_with_dashes( $single->post_title );
$this_slug = $slug_base;
$slug_num = 1;
// Check if the slug already exists, if it does, we add an incremental integer ot the end of it
while (in_array( $this_slug, $slug_done ) )
{
$this_slug = $slug_base . ‘-‘ . $slug_num;
$slug_num++;
}
$slug_done[] = $this_slug;
// We are happy with our slug, update the database table
$wpdb->query( ”
UPDATE
`” . $wpdb->posts . “`
SET
`post_name` = ‘” . $this_slug . “‘
WHERE
`ID` = ‘” . $single->ID . “‘
LIMIT 1
” );
}

This code must be inserted in the wp-config.php file before the ending PHP tag and save it followed by reloading the front of the website. Once it loads successfully remove the code from the wp-config.php file. The problem will be solved now.

Conclusion

The above mentioned procedure will be helpful for any user thinking of migrating Drupal to WordPress but possession of programming knowledge is compulsory for successful completion of the process. Amateurs who do not have such skills can call tel:+1.415.548.6170 and request a quote for professional help in solving the problem.

Leave a Reply