Development

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

How to Programmatically Enroll a User in a Course

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

  1. Returns 0 unless the course (or bundle) is published or private.
  2. Runs the tutor_allow_course_enrollment filter; a WP_Error stops it.
  3. If the user is already enrolled (active), returns the existing enrollment ID instead of creating a duplicate.
  4. Creates a tutor_enrolled post: the student is post_author, the course is post_parent.
  5. Fires tutor_after_enroll (course ID, enrollment ID) unless $fire_hook is false.

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 false as 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() returns 0.
  • 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.

Vishavjeet
· Enterprise LMS Architect | WordPress & LMS Expert | WooCommerce | AI Automation

I’m Vishavjeet Choubey, an Enterprise LMS Architect with 10+ years of experience helping businesses, coaches, universities, and online academies build and scale powerful digital learning platforms. I specialize in enterprise LMS architecture, WordPress, WooCommerce, LifterLMS, LearnDash, Moodle, custom plugin development, and AI-driven automation. I build scalable LMS platforms, high-converting eCommerce solutions, and custom WordPress applications designed to improve user experiences, streamline operations, and drive measurable business growth. My approach combines deep WordPress and LMS engineering expertise with modern AI automation to reduce manual work, improve efficiency, and help organizations launch, scale, and grow faster.

Keep reading

Need a custom Tutor LMS solution?

Tell us what you are building or what is broken. A Tutor LMS developer reads every inquiry and replies with next steps and a free estimate.

Get a free Tutor LMS consultation

Tell us what you are building or fixing. A Tutor LMS developer replies with honest advice and next steps.

  • No obligation
  • Tutor LMS and Pro specialists
  • Your details stay private

This form is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.