Skip to main content

The default behaviour of LearnDash LMS is set to navigate the learner back to the lesson frontpage once all included topic pages have been marked as complete, as opposed to transitioning the learner straight onto the end of lesson quiz (if you have one in place).

This may not be the user experience that you had in mind, and would much prefer to have the learner be directed straight to the lesson quiz than be sent back to the incomplete lesson page itself, to then have to click on the quiz link themselves manually.

Thankfully there is a custom function written by LearnDash which can override this behaviour. Just add the following code snippet to your theme’s functions.php file:

 

/** LearnDash - Redirect the user to a Lesson Quiz when they have completed the last Lesson Topic
* This snippet hooks into the 'learndash_completion_redirect' to allow override of the redirect link
*/
add_filter( 'learndash_completion_redirect', function( $link, $post_id ) {

// We only want to do this for Topics. But the below code can be adapted to work for Lessons
if ( get_post_type( $post_id ) == 'sfwd-topic' ) {

// First we get the topic progress. This will return all the sibling topics.
// More important it will show the next item
$progress = learndash_get_course_progress( null, $post_id );

// Normally when the user completed topic #3 of #5 the 'next' element will point to the #4 topic.
// But when the student reaches the end of the topic chain it will be empty.
if ( !empty( $progress ) && ( isset( $progress['next'] ) ) && ( empty( $progress['next'] ) ) ) {

// So this is where we now want to get the parent lesson_id and determine if it has a quiz
$lesson_id = learndash_get_setting( $post_id, 'lesson' );
if ( !empty( $lesson_id ) ) {
$lesson_quizzes = learndash_get_lesson_quiz_list( $lesson_id );
if ( !empty( $lesson_quizzes ) ) {

// If we have some lesson quizzes we loop through these to find the first one not completed by the user.
// This should be the first one but we don't want to assume.
foreach( $lesson_quizzes as $lesson_quiz ) {
if ( $lesson_quiz['status'] == 'notcompleted' ) {
// Once we find a non-completed quiz we set the $link to the quiz
// permalink then break out of out loop
$link = $lesson_quiz['permalink'];
break;
}
}
}
}
}
}

// Always return $link
return $link;

}, 20, 2);
Chris Hodgson

Chris Hodgson is an award-winning Digital learning Solutions Developer and Director of Discover eLearning Ltd. He supports organisations of all sizes to implement engaging, meaningful, and impactful eLearning solutions. Chris has 15 years’ experience working in both private and public sector learning and development.

One Comment

  • Jordan Bickel says:

    Just discovered this website and wow! What a tremendous help!
    This is exactly what I’m trying to do – I tried inputting the above code to the theme’s function.php file but had no luck. Any tips?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.