2019-ebook-3d-onblue4
Everything you need to know about building mobile apps

Learn what a mobile app can do for your business, how to build a great mobile app, and much more. Enter your email below for our free ebook.

Function to Enroll a User into a LearnDash Course

To enroll or unenroll a user from a LearnDash course programatically (using code), you use the ld_update_course_access() function.

Enroll

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

This function takes 3 arguments, the user id, course id, and whether you’d like to remove them from the course.

To get the course id, it’s helpful to use the learndash_get_course_id() function. This allows you to pass the ID of a lesson, topic, or quiz that belongs to the course, and then gives you the course id.

Here’s an example:

$user_id = 12;
$lesson_id = 321;

$course_id = learndash_get_course_id( $lesson_id );

// add user to course
ld_update_course_access( $user_id, $course_id, false );

Unenroll

To unenroll, you just add true for the last argument.

ld_update_course_access( $user_id, $course_id, true );

No Code Enrollment

To enroll or unenroll a user in a LearnDash course with no code, visit their user profile in the admin and scroll down to the enrollment section. Click the course name and use the arrows to change course access, then save.

LearnDash enroll or unenroll a user in a course.

2 Comments

  1. Tobias on January 26, 2021 at 7:42 am

    Where do I have to place that code?

    • Scott Bolinger on January 26, 2021 at 9:20 am

      You should put that code in a hook inside a custom plugin.

Leave a Comment





Scott Bolinger