Skip to content

Instantly share code, notes, and snippets.

@actual-saurabh
Created August 23, 2018 15:02
Show Gist options
  • Save actual-saurabh/e9722d81353d5cb760122c1ba2f96ac1 to your computer and use it in GitHub Desktop.
Save actual-saurabh/e9722d81353d5cb760122c1ba2f96ac1 to your computer and use it in GitHub Desktop.
LifterLMS Course redirect to first incomplete lesson
<?php // Do not copy this line
// Copy from under this line and paste into your child theme's functions.php
add_action( 'template_redirect', 'lifterlms_course_continue_redirect' );
if ( ! function_exists( 'lifterlms_course_continue_redirect' ) ) {
function lifterlms_course_continue_redirect( $post_id = null, $student = null, $progress = null ) {
if ( ! $post_id ) {
$post_id = get_the_ID();
if ( ! $post_id ) {
return '';
}
}
$course = llms_get_post( $post_id );
if ( ! $course || ! is_a( $course, 'LLMS_Post_Model' ) ) {
return '';
}
if ( $course->get( 'type' ) != 'course' ) {
return '';
}
if ( ! $student ) {
$student = llms_get_student();
}
if ( ! $student || ! $student->exists() || ! llms_is_user_enrolled( $student->get_id(), $course->get( 'id' ) ) ) {
return '';
}
if ( is_null( $progress ) ) {
$progress = $student->get_progress( $course->get( 'id' ), 'course' );
}
if ( 100 == $progress ) {
return '';
} else {
$lesson = $student->get_next_lesson( $course->get( 'id' ) );
if ( $lesson ) {
wp_safe_redirect( get_permalink( $lesson ) );
exit();
}
}
}
}// End if().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment