# LearnDash: Encouraging Email When a Quiz Is Failed

> Emails students who did not pass a LearnDash quiz with their score and a link to try again, so they do not give up.

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

## Code

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

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

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

    if ( ! $quiz_id ) {
        return;
    }

    $key = 'wpide_ld_fail_' . $user->ID . '_' . $quiz_id;

    if ( get_transient( $key ) ) {
        return;
    }

    set_transient( $key, 1, DAY_IN_SECONDS );

    wp_mail(
        $user->user_email,
        sprintf( 'Almost there: %s', get_the_title( $quiz_id ) ),
        sprintf(
            "Hi %s,\n\nYou scored %s%% on \"%s\". That is not a pass yet, but you can review the lessons and try again any time:\n%s\n\nYou've got this!",
            $user->first_name ? $user->first_name : $user->display_name,
            isset( $quizdata['percentage'] ) ? round( (float) $quizdata['percentage'] ) : 0,
            get_the_title( $quiz_id ),
            get_permalink( $quiz_id )
        )
    );
}, 10, 2 );
```

## Notes

Sent at most once a day per quiz and student, so repeated attempts do not flood their inbox.

## How to use it

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