# LifterLMS: Encouraging Email When a Quiz Is Failed

> Emails students who fail a LifterLMS quiz with their grade and a link to review the lesson and try again.

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

## Code

```php
if ( ! function_exists( 'llms_get_student' ) ) {
    return;
}

add_action( 'lifterlms_quiz_failed', function ( $user_id, $quiz_id, $attempt ) {
    $user = get_userdata( $user_id );
    $key  = 'wpide_llms_fail_' . (int) $user_id . '_' . (int) $quiz_id;

    if ( ! $user || get_transient( $key ) ) {
        return;
    }

    set_transient( $key, 1, DAY_IN_SECONDS );

    $grade  = is_object( $attempt ) && method_exists( $attempt, 'get' ) ? round( (float) $attempt->get( 'grade' ) ) : null;
    $lesson = (int) get_post_meta( $quiz_id, '_llms_lesson_id', true );

    wp_mail(
        $user->user_email,
        sprintf( 'Keep going: %s', get_the_title( $quiz_id ) ),
        sprintf(
            "Hi %s,\n\n%s Review the lesson and give it another go:\n%s",
            $user->first_name ? $user->first_name : $user->display_name,
            null === $grade ? 'You did not pass the quiz this time.' : sprintf( 'You scored %d%% on "%s", just short of a pass.', $grade, get_the_title( $quiz_id ) ),
            get_permalink( $lesson ? $lesson : $quiz_id )
        )
    );
}, 10, 3 );
```

## Notes

Sent at most once a day per student and quiz.

## How to use it

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