# LearnDash: Give a Role When a Quiz Is Passed

> Adds a role to students when they pass a chosen LearnDash quiz, for example a "Certified" role that unlocks a members area.

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

## Code

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

// Quiz ID => role to add.
$wpide_ld_quiz_roles = array(
    123 => 'contributor',
);

add_action( 'learndash_quiz_completed', function ( $quizdata, $user ) use ( $wpide_ld_quiz_roles ) {
    if ( ! $user instanceof WP_User || empty( $quizdata['pass'] ) ) {
        return;
    }

    $quiz_id = is_object( $quizdata['quiz'] ?? null ) ? (int) $quizdata['quiz']->ID : (int) ( $quizdata['quiz'] ?? 0 );
    $role    = $wpide_ld_quiz_roles[ $quiz_id ] ?? '';

    if ( $role && get_role( $role ) && ! in_array( $role, (array) $user->roles, true ) ) {
        $user->add_role( $role );
    }
}, 10, 2 );
```

## Notes

The role is added alongside the student's current role. Make sure the role exists (for example with a role editor plugin).

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "LearnDash: Give a Role When a Quiz Is Passed" 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/
