# LearnDash: Change Role After Course Completion

> Adds a role (or replaces the current one) when a student completes a chosen course, such as promoting trainees to "Instructor".

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

## Code

```php
if ( ! defined( 'LEARNDASH_VERSION' ) ) {
    return;
}

// Course ID => role.
$wpide_ld_course_roles = array(
    123 => 'author',
);
$wpide_ld_replace_role = false;

add_action( 'learndash_course_completed', function ( $data ) use ( $wpide_ld_course_roles, $wpide_ld_replace_role ) {
    $user      = $data['user'] ?? null;
    $course_id = isset( $data['course']->ID ) ? (int) $data['course']->ID : 0;
    $role      = $wpide_ld_course_roles[ $course_id ] ?? '';

    if ( ! $user instanceof WP_User || ! $role || ! get_role( $role ) || user_can( $user, 'manage_options' ) ) {
        return;
    }

    if ( $wpide_ld_replace_role ) {
        $user->set_role( $role );
    } elseif ( ! in_array( $role, (array) $user->roles, true ) ) {
        $user->add_role( $role );
    }
} );
```

## Notes

Set $wpide_ld_replace_role to true to swap the role instead of adding it. Administrators are never changed.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "LearnDash: Change 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/
