Migration from Moodle to LearnDash

Posted on August 22, 2014

Posted by Henry Tam

In this article, I will detail the user enrollment data migration from Moodle to LearnDash.

I had a client with an established online learning website on the Moodle platform. While everything was working fine, the Moodle system was just not ideal in terms of user flow. The user interface out of the box was also dated. I reviewed the work necessary to configure Moodle to what the client wants. I then researched the market for a LMS, and came up with the recommendation to simply purchase LearnDash. This means lower development effort for me, lower cost of implementation, and faster time to go live for the client. However, one of the biggest challenges would be to migrate existing user and enrollment data from the Moodle system over to the LearnDash system. In the remaining article, I will review the technical details of how to complete the user enrollment data migration.

The approach

What I will do here is figure out how to import users into LearnDash. With that piece figured out, I can investigate the data model of Moodle and extract data into a format which can be used to import into LearnDash.

The migration from Moodle to LearnDash

LearnDash has an API to enroll and unenroll users. Unfortunately, it only works for one user at a time. So the solution will require looping through a list of users and enrolling them one by one.

This API function takes a WordPress user id and LearnDash course id. The last optional parameter specifies if you want to enroll (set to false) or unenroll (set to true). By default, if no value is passed, false is used, which means enroll the user.

ld_update_course_access($user_id, $course_id, $remove = false);

With this API function figured out, the next step is to generate a list of users and associated LearnDash course ids used to call this function. Here are the relevant Moodle database tables:

 wp_users -- WordPress user table
 mdl_user -- Moodle user table
 mdl_course -- Moodle course table
 mdl_user_enrolments -- Moodle join table between a Moodle course and Moodle user
 mdl_enrol -- Moodle join table between course and enrollment

Here is the complete query to run on a MySQL database to extract the user and associated LearnDash course id. The CASE WHEN code in the SELECT section is used to translate between Moodle course id and LearnDash course id. You will need to look these ids up and replace it in the script before running it. I only have three classes to migrate. If you have more or less, you will need to adjust accordingly.

SELECT example_u.ID as user_id,
 /* Moodle course 2 translates to 3485 in LearnDash */
 WHEN mood_c.id = 2 THEN 3485
 /* Moodle course 3 translates to 3522 in LearnDash */
 WHEN mood_c.id = 3 THEN 3522
 /* Moodle course 4 translates to 3528 in LearnDash */
 WHEN mood_c.id = 4 THEN 3528
 ELSE -1
 END AS course_id
 md_example.mdl_user mood_u,
 wp_example.example_users example_u,
 md_example.mdl_user_enrolments mood_ue,
 md_example.mdl_enrol mood_e, 
 md_example.mdl_course mood_c 
WHERE mood_u.email = example_u.user_email
 and mood_u.id = mood_ue.userid
 and mood_ue.enrolid = mood_e.id
 and mood_e.courseid = mood_c.id
 ORDER BY example_u.ID;

Running this query will result in output like the below. Each line represents one user/course enrollment. The first column is the Moodle / WordPress user id, the second column is the LearnDash course id. You will need to save this data into a text file (moodle_enrollment_data.txt) for the next step of importing it into the LearnDash system.


With the user / course enrollment data file prepared. The next step is to run this php script which will call the LearnDash ld_update_course_access API to import users. Create a WordPress page called ‘enroll_moodle_users’ from the WordPress admin pages with a custom template called, ‘page-enroll_moodle_users.php’ in /home/USER/public_html/wp-content/themes/THEME with contents:

$lines = file('/home/USER/public_html/wp-content/themes/THEME/moodle_enrollment_data.txt');
 $mode = $_GET["mode"];
 $results = array();
if (!isset($mode)) die ('Operating mode not set<br/>');
 if ($lines == FALSE) die ('Error reading Moodle data file<br/>');
print "Enroll Moodle Users<br/>";
 print "Mode:" . $mode . "<br/>";
if ('enroll' === $mode) {
    foreach ($lines as $line_num => $line) {
        $items = explode (',', $line);
        array_push($results, ld_update_course_access($items[0], $items[1], false));
 } elseif ('unenroll' === $mode) {
    foreach ($lines as $line_num => $line) {
        $items = explode (',', $line);
        array_push($results, ld_update_course_access($items[0], $items[1], true));
print 'Results:';
 print_r ($results);
 print "Done";
Enroll Moodle users page

Enroll Moodle users page






Next. Open your web browser and go to this page to run the script.



Running PHP import script

Running PHP import script





The output of the script will show the list of WordPress user ids and LearnDash course ids which were enrolled. For testing, you can run the script with the mode parameter changed to unenroll which will unenroll the list of users/course combinations.

At this point, all your user enrollment data will be in the LearnDash system. To confirm, log in as one of the users and verify that that user has access to the LearnDash course as they did in the Moodle system.

Henry Tam is available for consultation for user enrollment data migration from Moodle to LearnDash. Please navigate over to the contact tab to get in touch. Thank you!



1. This article assumes the primary site is on the WordPress platform which communicates with a separate Moodle system / database. In this system, the wpmoodle plugin was used. If you had a complete Moodle system to migrate to a complete LearnDash system, then you will need to create all the users first before following the steps above.

2. LearnDash does not currently support migration from Moodle to LearnDash. The process detailed here is custom work using a sql query and php script to export / import user enrollment data.

3. LearnDash does not currently have a message board feature.

4. The ORDER BY in the sql script is not necessary. It is okay to remove it for better performance.

5. Be sure to remove the enroll_moodle_users page from the admin panel when done.

Migration from Moodle to LearnDash was last modified: May 14th, 2016 by Henry Tam

Leave a Reply

Your email address will not be published. Required fields are marked *