How to Programmatically Enroll a User in a Course
Enroll users in Tutor LMS courses from PHP with EnrollmentModel::do_enroll(), activate paid-course enrollments, avoid duplicates and run it in bulk safely.
Checked against the plugin source: Tutor LMS 4.1.3, Tutor LMS Pro 4.1.3

Imports, integrations and custom sign-up flows often need to enroll a user without the normal checkout. Tutor LMS does this with one function. This guide is verified against Tutor LMS 4.1.3.
The function
use Tutor\Models\EnrollmentModel;
$enrollment_id = EnrollmentModel::do_enroll( $course_id, $order_id = 0, $user_id = 0, $fire_hook = true );
tutor_utils()->do_enroll() forwards to the same method. It returns the enrollment ID, or 0 if nothing was created.
What it does
- Returns
0unless the course (or bundle) is published or private. - Runs the
tutor_allow_course_enrollmentfilter; aWP_Errorstops it. - If the user is already enrolled (active), returns the existing enrollment ID instead of creating a duplicate.
- Creates a
tutor_enrolledpost: the student ispost_author, the course ispost_parent. - Fires
tutor_after_enroll(course ID, enrollment ID) unless$fire_hookisfalse.
Paid courses start as pending
The status depends on the course. For a free course the enrollment is completed (active). For a paid course it is pending, because Tutor LMS expects an order to complete it. If you are granting access yourself, activate it:
use Tutor\Models\EnrollmentModel;
function tle_grant_course( $course_id, $user_id ) {
$enrollment_id = EnrollmentModel::do_enroll( $course_id, 0, $user_id );
if ( ! $enrollment_id ) {
return false;
}
if ( 'completed' !== get_post_status( $enrollment_id ) ) {
// Fires tutor_enrollment/after/completed for each ID.
EnrollmentModel::update_enrollments( 'completed', array( $enrollment_id ) );
}
return $enrollment_id;
}
Check afterwards with tutor_utils()->is_enrolled( $course_id, $user_id ); see checking enrollment.
When to pass an order ID
Pass a WooCommerce, EDD or native order ID only when the enrollment really comes from that order. Tutor LMS then stores the order and product IDs on the enrollment, so refunds and cancellations of that order can find it. For a manual grant, leave it at 0.
Bulk enrollment
- Run large jobs from WP-CLI or a background queue, not a page request.
- Process users in batches of a few hundred and log results.
- Decide whether you want hooks to fire. Hooks trigger emails and integrations; for a silent migration, pass
falseas the fourth argument and handle side effects yourself. - For CSV imports without code, Tutor LMS Pro has manual and bulk enrollment tools; see bulk enrolling students.
Common mistakes
- Course is a draft:
do_enroll()returns0. - Student cannot see a paid course: the enrollment is still pending.
- Calling too early: run on or after
init, when Tutor LMS is loaded.
Frequently asked questions
Can I enroll via the REST API instead?
Yes, with Tutor LMS Pro: POST /wp-json/tutor/v1/enrollments calls the same function. See the Pro REST API guide.
How do I remove an enrollment?
tutor_utils()->cancel_course_enrol( $course_id, $user_id, 'cancel' ) changes its status; passing 'delete' removes it and fires tutor_after_enrollment_deleted.
Need an enrollment integration? Our plugin development team builds them.
Save this practical guide
Add us as a preferred Google source, or ask an AI assistant to summarize this page and link back to the original guide when it is useful.
