LifterLMS: Add a Role After Course Completion
Adds a role to students when they complete a chosen LifterLMS course, for example to unlock a graduates-only area.
Opens WPIDE on
Code
if ( ! function_exists( 'llms_get_student' ) ) {
return;
}
// Course ID => role to add.
$wpide_llms_course_roles = array(
123 => 'author',
);
add_action( 'lifterlms_course_completed', function ( $user_id, $course_id ) use ( $wpide_llms_course_roles ) {
$role = $wpide_llms_course_roles[ (int) $course_id ] ?? '';
$user = get_userdata( $user_id );
if ( $role && $user && get_role( $role ) && ! in_array( $role, (array) $user->roles, true ) ) {
$user->add_role( $role );
}
}, 10, 2 );
Notes
The role is added next to the student's current role and must already exist.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.