LMS & eLearning

Snippet: Auto-Enroll New WordPress Users Into a LearnDash Course

A tested snippet that enrolls every newly registered WordPress user into one or more LearnDash courses, with role filtering, a WooCommerce variant, a group alternative, a back-fill loop and testing steps.

Snippet: Auto-Enroll New WordPress Users Into a LearnDash Course

A membership site, an employee portal or a free onboarding course often wants every new account to start with a course already in place. LearnDash has no "enroll all new users" setting; what it has is a one-line enrollment function and WordPress's registration hook, which together make the snippet below. It runs on every registration path that calls the standard WordPress function, including the LearnDash registration form, WooCommerce checkout and most membership plugins.

Before you add code

Two built-in options may already cover the need. A course in open access mode is available to everyone without enrollment, which is simpler if the course is free and public. And the free access mode with the Take this Course button lets users self-enroll in one click. Use the snippet when you need the course to appear on the user's profile and in their progress from the moment the account exists, without an action from them.

The snippet

<?php
/**
 * Enroll newly registered users into specific LearnDash courses.
 */
add_action( 'user_register', 'wwc_ld_enroll_new_user', 20, 1 );

function wwc_ld_enroll_new_user( $user_id ) {
    $user_id = absint( $user_id );
    if ( 0 === $user_id || ! function_exists( 'ld_update_course_access' ) ) {
        return;
    }

    // Course IDs to enroll every new user into.
    $course_ids = array( 123, 456 );

    foreach ( $course_ids as $course_id ) {
        $course_id = absint( $course_id );
        if ( 'sfwd-courses' !== get_post_type( $course_id ) ) {
            continue;
        }
        ld_update_course_access( $user_id, $course_id );
    }
}

Replace the IDs with your course IDs, which are shown in the URL of the course edit screen. Add the code to a small site-specific plugin or your child theme's functions file. It does not need to run on activation; it affects accounts created from then on.

How it works

WordPress fires user_register once a new user row exists, passing the user ID. ld_update_course_access( $user_id, $course_id ) is the function LearnDash itself uses for enrollment: it writes the access meta, records the activity row and fires learndash_update_course_access, and it returns false harmlessly if the user is already enrolled. The post type check avoids enrolling into a deleted or mistyped ID. Priority 20 lets other plugins finish setting up the user first. Our guide on manual enrollment explains what the function writes.

Only certain roles

To enroll only users registered with a particular role, check the role inside the callback. On user_register the role has normally been set, but a plugin that assigns roles later should be handled on set_user_role instead:

$user = get_userdata( $user_id );
if ( ! $user || ! in_array( 'subscriber', (array) $user->roles, true ) ) {
    return;
}

Groups as an alternative

If the set of starter courses may change, enroll new users into a group instead and manage the courses on the group. Replace the loop with ld_update_group_access( $user_id, $group_id ), which returns true on success. Changing the group's courses later updates every member, which direct enrollments cannot do. Our guide to creating groups covers the group side.

Enrolling only buyers from WooCommerce

If accounts are created in several places and only one of them should enroll, hook that path instead of the general registration action. WooCommerce fires woocommerce_created_customer with the new customer ID whenever checkout or the My Account form creates an account, so the same callback body on that hook enrolls shoppers and leaves staff accounts created in the admin alone:

add_action( 'woocommerce_created_customer', 'wwc_ld_enroll_new_user', 20, 1 );

Because the callback already validates the ID and the course, nothing else changes. If a paid course should come with the purchase rather than with the account, use the LearnDash WooCommerce integration and its Related Courses field instead; that is what it is for, and it handles refunds, which this snippet does not.

Back-filling existing users

The hook only fires for accounts created after the snippet is in place. To bring existing users up to the same state, run a one-off loop from WP-CLI, in batches, so a site with thousands of users does not time out:

wp eval '
$paged = 1;
do {
    $users = get_users( array( "role" => "subscriber", "number" => 200, "paged" => $paged, "fields" => "ID" ) );
    foreach ( $users as $user_id ) {
        wwc_ld_enroll_new_user( (int) $user_id );
    }
    $paged++;
} while ( count( $users ) === 200 );
'

Reusing the same callback keeps the rules in one place. The enrollment function returns false for anyone already enrolled, so the loop can be run twice without side effects. The enrollment date for back-filled users is the day you run the loop, not their registration date; if drip or expiry should count from registration, pass the registration date to ld_course_access_from_update() afterwards, as the manual enrollment guide shows.

Testing the snippet

  1. Install the snippet on a staging copy and confirm the course IDs point at the right courses by opening each edit screen.
  2. Register a new account through the front-end form your site really uses, not the admin Add New screen, so any plugin that intervenes in registration is part of the test.
  3. Open the new user under Users and check the LearnDash section lists the courses with today's enrollment date.
  4. Log in as that user and open a course page; it should show progress and the first lesson rather than an enrollment button.
  5. Register a second account with a role the snippet should ignore, if you added the role check, and confirm it received nothing.

If step 3 shows no course, add a temporary error_log() line at the top of the callback to confirm the hook fires at all; a registration plugin that creates users with a direct database insert rather than the standard function will skip the hook, and then the plugin's own "after registration" action is the one to use.

Caveats

  • Users created by an import or by WP-CLI also trigger the hook, so a bulk import enrolls everyone. Deactivate the snippet during an import if that is not wanted.
  • Enrollment date is the registration time, which is what drip and expiry count from.
  • The snippet does not send an email. Use the Notifications add-on's enroll into course trigger if learners should be told.
  • Existing users are not touched. For them, run a one-off loop over user IDs with the same function from WP-CLI.

When to get help

This is a copy-and-adjust snippet. If enrollment rules depend on form fields, purchase data or an external system, or if thousands of existing users must be back-filled with correct dates, that is a small integration project our LearnDash development team handles regularly; a LearnDash developer can also wire the same logic into a membership plugin's own hooks. See our LearnDash services for the broader picture.

Frequently asked questions

Does LearnDash auto enroll new users without code?

Not for a specific course. Open courses need no enrollment, and administrators can be auto-enrolled by a setting, but enrolling every new account into a chosen course needs the snippet or a group-based alternative.

Will this enroll users who register through WooCommerce?

Yes. WooCommerce creates accounts with the standard WordPress function, which fires the hook.

Can I enroll existing users with this snippet?

Not automatically. Loop over existing user IDs once, calling the same enrollment function, preferably from WP-CLI in batches.

What if the course has a price?

The function enrolls regardless of price type. If the course is paid, the snippet gives it away, so point it at free or closed courses only.

Add WorldWin Coder to your preferred sources

Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.

· Developers and project managers

Articles written and reviewed by the developers and project managers at WorldWin Coder, who build and maintain websites, web applications and mobile apps on WordPress, Moodle, PHP, Laravel, CodeIgniter and Flutter.

Keep reading

Have something to build, fix or move?

Tell us where you are. A developer reads every inquiry, and you get next steps and a free quote within one business day.