Tutor LMS: Grant a Role to Course Graduates
Adds a role to students when they complete a chosen Tutor LMS course, for example to open a graduates-only forum or downloads area.
Opens WPIDE on
Code
if ( ! function_exists( 'tutor_utils' ) ) {
return;
}
// Course ID => role.
$wpide_tutor_course_roles = array(
123 => 'author',
);
add_action( 'tutor_course_complete_after', function ( $course_id, $user_id = 0 ) use ( $wpide_tutor_course_roles ) {
$role = $wpide_tutor_course_roles[ (int) $course_id ] ?? '';
$user = get_userdata( $user_id ? (int) $user_id : get_current_user_id() );
if ( $role && $user && get_role( $role ) && ! in_array( $role, (array) $user->roles, true ) ) {
$user->add_role( $role );
}
}, 10, 2 );
Notes
The role must already exist and is added next to the student's current role.
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.