End of Year Sale: 30% off your first year on annual plans. Use code AP5-30-OFF See plans
WordPress

Function to Enroll a User into a LearnDash Course

Updated 1 min read
LearnDash logo

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 responses to “Function to Enroll a User into a LearnDash Course”

  1. Tobias Avatar

    Where do I have to place that code?

    1. Scott Bolinger Avatar

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

WordPress

iOS & Android app builder for WordPress

Ready to build your app?

Turn your WordPress site into iOS and Android apps with push notifications on every plan.

AppPresser End of Year Sale: 30% off AppPresser subscriptions, now through Dec 31. Two phones show sample news and events apps built with AppPresser.

End of Year Sale

30% off your first year

On Standard, Professional and Advanced annual plans. Enter the code at checkout. Ends December 31, 2026 at 11:59 PM ET.

Code AP5-30-OFF
See pricing