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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.