# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Courses & Community](https://wpide.com/library/category/learning/)
- Tags: tutor, roles, courses
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/tutor-role-after-course-completion/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Tutor LMS: Grant a Role to Course Graduates" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
