LearnDash, by default, has different enrollment types: Open, Free, Buy Now, Recurring, and Closed.
When you set a course access mode to Open, any user account registered on your website will be automatically enrolled in that course.
Non-logged-in visitors will simply see all the course content. Just LearnDash won’t track any progress as there is no user account to assign the course completion yet.
With the remaining access modes, your users must complete some action in order to enroll in the courses.
The advantage is that not everyone will be part of the course, allowing you to keep a cleaner record of the enrolled users.
The disadvantage is that it may create friction.
Going on a different route, let’s say your online academy is receiving deal offers from related brands that have the potential to have a significant and positive impact on your business.
And let’s say that part of the deal is to allow access to a set of courses ‘for free’ to employees of that brand.
But this set of courses is Buy Now or Closed, and there is no clear way to make a frictionless enrollment process for the specific group of users.
You could create a free group that allows automatic access to any user who enrolls in it for a specific set of courses.
The problem with that is that even as hard as you try, with the base LearnDash setup, any user on the website may run into the group and would have free access to the courses.
That wouldn’t be the ideal outcome.
You could also prevent access to the group with a membership plugin like Paid Memberships Pro, but again, you’d need to indicate what users are part of that membership.
Or you could even manually enroll the users in the group to control who has access to the group and who does not.
An alternative, automated, and frictionless method is to enroll the users in a course or group as they sign up to your website but filter by their email address domain if they are eligible for auto-enrollment.
Saying your deal is with Delta Airlines, whose domain is www.delta.com, check any email address from @delta.com for the account creation. If the email domain matches, then auto-enroll the newly created user in a specific group with ID 5.
The code to achieve that would go something like so:
function add_user_to_group_based_on_email( $user_id ) {
$user_info = get_userdata( $user_id );
$email = $user_info->user_email;
// Get domain from the email
$domain = array_pop(explode('@', $email));
// Define your specific domain and group ID
$specific_domain = 'example.com'; // Change this to your specific domain
$group_id = 123; // Change this to your LearnDash group ID
// Check if domain matches
if( $domain === $specific_domain ) {
// Use the ld_update_group_access function to add the user to the group
ld_update_group_access( $user_id, $group_id );
}
}
add_action( 'user_register', 'add_user_to_group_based_on_email' );
The first step is to hook into the user_register action.
That allows you to intercept all user registrations.
Get the user by ID and get the user’s email.
Use a combination of explode and array_pop first to make the email address an array separated by the @ symbol. Then only stay with the last element of the array, which is the actual domain name.
Indicate the domain you want to filter in a variable and the ID of the group you want to auto-enroll the user into.
Then, evaluate the domains. If there is a match, leverage the ld_update_group_access() function provided by LearnDash to update the access for the specific user.
That’s about it. Now over to you.
Let me know in the comments below if you have any questions or if you want to add something.
Have a good one!