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

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

## Code

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

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "LifterLMS: Add a Role After Course Completion" 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/
