-
-
Save lucanos/bb53808e34f78e85c7b17fec32793589 to your computer and use it in GitHub Desktop.
Get LearnDash next lesson link or first lesson link if course not started
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ld_next_lesson_link( $course_id = null ) { | |
global $post; | |
$user = _wp_get_current_user(); | |
if( is_null( $course_id ) ) { | |
$course_id = learndash_get_course_id( $post ); | |
} | |
if( !$course_id || !isset( $user->ID ) ) { | |
// User Not Logged In OR No Course Identified | |
return false; | |
} | |
$lessons = learndash_get_lesson_list( $course_id ); | |
if( !$lessons ) { | |
// No Lesson | |
return false; | |
} | |
$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; | |
} | |
} | |
} | |
} elseif( isset ( $first_lesson->ID ) ) { | |
// get first lesson link | |
$lesson_id = $first_lesson->ID; | |
} | |
if( !$lesson_id ) { | |
// No Lesson ID | |
return false; | |
} | |
if( 'sfwd-lessons' != get_post_type( $lesson_id ) ) { | |
// ID not for a Learndash Lesson | |
return false; | |
} | |
$link = get_post_permalink( $lesson_id ); | |
return $link; | |
} | |
function shortcode_ld_next_lesson_link( $atts , $content = 'Next Lesson' ){ | |
extract(shortcode_atts(array( | |
'course_id' => null , | |
'class' => 'learndash-next-lesson' | |
), $atts)); | |
$url = ld_next_lesson_link( $course_id ); | |
if( $url ) { | |
return '<a href="'.$url.'" class="'.$class.'">'.$content.'</a>'; | |
} | |
return false; | |
} | |
add_shortcode('ld_next_lesson_link', 'shortcode_ld_next_lesson_link'); |
Thanks so much for taking the time to write this script.
If I want the next topic or element in Learndash, do I simply change the 'Lesson' to 'Topic'?
Thank you very much! It solves perfectly my problem!
:)
Thank you, this was very helpful. I added an additional function to grab the first topic id from the lesson id returned in your function.
`function ld_first_topic_id( $lesson_id = null, $course_id = null ) {
if( empty($lesson_id) || empty($course_id) ) {
return false;
}
$topics = learndash_get_topic_list( $lesson_id, $course_id );
if( empty($topics) ) {
return false;
}
$first_topic = reset( $topics );
if( empty($first_topic->ID) || 'sfwd-topic' != get_post_type( $first_topic->ID ) ) {
return false;
}
return $first_topic->ID;
}`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your code is very well written. I started learning programming not so long ago, in the 10th grade. My mother does not approve of my hobbies and does not believe that I can make money "playing on the computer." She makes me write and even enrolled in extracurricular activities. Thanks to https://gradesfixer.com/free-essay-examples/millennial-generation/ for helping me write these silly talks and abstracts. But I will still study further and move towards my dream. All these thanks to your lessons. I hope that only I was so unlucky with my ancestors. I have to say that I go to the library to study, while I myself sit in the Internet club and study.