Migrating users between Wordpress installations

If you find yourself working on a forked Wordpress installation and need to migrate users from your production fork to your brand new site (based on the old fork) when putting it into production, here’s a snippet to to just that.

This snippet will also work if you simply want to import users from another Wordpress database, although since the password salt is different, the users will need to reset their password.

ALTER TABLE NEWDB.wp_users ADD UNIQUE( user_login);
ALTER TABLE NEWDB.wp_usermeta ADD UNIQUE( user_id, meta_key);
ALTER TABLE NEWDB.wp_users ADD old_id BIGINT NULL;

INSERT IGNORE INTO NEWDB.wp_users
SELECT '' as id, user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name, id as old_id FROM OLDDB.wp_users ;

INSERT IGNORE INTO NEWDB.wp_usermeta
SELECT '' as umeta_id, newuser.id as user_id, oldusermeta.meta_key, oldusermeta.meta_value
    FROM OLDDB.wp_usermeta as oldusermeta
    INNER JOIN NEWDB.wp_users AS newuser on newuser.old_id=oldusermeta.user_id
    WHERE newuser.old_id IS NOT NULL;

Add a comment