Skip to content

Instantly share code, notes, and snippets.

@adeel-raza
Last active August 16, 2021 01:06
Show Gist options
  • Save adeel-raza/099b437feebbfd7c5ebafceea1520755 to your computer and use it in GitHub Desktop.
Save adeel-raza/099b437feebbfd7c5ebafceea1520755 to your computer and use it in GitHub Desktop.
Get LearnDash next lesson link or first lesson link if course not started
function ld_next_lesson_link() {
global $post;
$course_id = learndash_get_course_id( $post );
$user = _wp_get_current_user();
if( $course_id && isset( $user->ID ) ) {
$lessons = learndash_get_lesson_list( $course_id );
if( $lessons ) {
$first_lesson = reset($lessons);
}
$user_course_progress = get_user_meta( $user->ID, '_sfwd-course_progress', true );
if( isset( $user_course_progress[$course_id] ) ) {
$course_progress = $user_course_progress[$course_id];
// get first lesson link
if( !$course_progress['lessons'] && isset( $first_lesson->ID ) ) {
$lesson_id = $first_lesson->ID;
} else {
end( $course_progress['lessons'] );
$lesson_id = key( $course_progress['lessons'] );
foreach( $lessons as $key => $lesson ) {
if( $lesson->ID == $lesson_id ) {
$lesson_id = $lessons[$key+1]->ID;
break;
}
}
}
} else {
// get first lesson link
if( isset ( $first_lesson->ID ) ) {
$lesson_id = $first_lesson->ID;
}
}
if( $lesson_id && "sfwd-lessons" == get_post_type( $lesson_id ) ) {
$link = get_post_permalink( $lesson_id );
return $link;
}
}
}
@marsvieyra
Copy link

Hey brother, thanks so much for this! I have created a button on my Courses template page and inserted this - works awesomely!

Now, I would like to hide this button if a user has completed the course.... how can I do that?

@aleung2020-wp-dev
Copy link

Hi adeel-raza,

thanks for sharing. I'm looking at Learndash core files for a way to link lessons to a course. is it what your code "$first_lesson = reset($lessons);" is doing? I also need to link topics to lessons. If you have any idea on what Learndash functions can help achieve that I would greatly appreciate you pointing me in the right direction.
Lauren

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment