Skip to content

Instantly share code, notes, and snippets.

@lucanos
Forked from adeel-raza/functions.php
Last active November 30, 2022 12:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucanos/bb53808e34f78e85c7b17fec32793589 to your computer and use it in GitHub Desktop.
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
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');
@marsvieyra
Copy link

hey brother - after adding this to the functions.php, how do I actually use this shortcode? I tried inserting it through Elementor using the Shortcode widget, and writing [ld_next_lesson_link] but it displays the text "[ld_next_lesson_link]". Thanks so much

@marsvieyra
Copy link

Oh! On my functions page, i see this error message after trying to save:

syntax error, unexpected ''class'' (T_CONSTANT_ENCAPSED_STRING), expecting ')'

@marsvieyra
Copy link

image

@lucanos
Copy link
Author

lucanos commented May 1, 2020

@marsvieyra - Was a small typo - fixed.

@marsvieyra
Copy link

thanks! it's strange, I still get an error on line 67.. any ideas why this could be?

image

@lucanos
Copy link
Author

lucanos commented May 1, 2020

@marsvieyra was missing a bracket. Pretty simple debugging actually - have a crack yourself at any other errors and share your solutions.

@marsvieyra
Copy link

Thanks for this! But it's still giving an error. I dont know how to code PHP.. i can understand some of it, but i'm no programmer... otherwise I would definitely give it a crack! I arrive at this post looking for a button for LearnDash... About the code: have you tried it in your WordPress installation? Does it work for you?

@lucanos
Copy link
Author

lucanos commented May 1, 2020

@marsvieyra, I forked the original gist over 8 months ago. I don't even remember what I was doing last week. So I can't remember how this gist behaved when I used it, or whether I tweaked it to get it to work. If you can give me line numbers for any further bugs, I'll try to fix them when I have time.

@marsvieyra
Copy link

thanks brother for taking the time to do this!!!
the error is on line 70 (after opening <?php):

syntax error, unexpected '.'

image

@lucanos
Copy link
Author

lucanos commented May 2, 2020

@marsvieyra I fixed that one already - grab the code from this page again

@marsvieyra
Copy link

oh! okay, just check and was able to update successfully on functions.php
how do I actually use this?
on Elementor, i added a Shortcode widget and entered [ld_next_lesson_link] - after saving the changes I don't see changes. Is this how I should use it?

@ZeeshanAslam123
Copy link

How to get only Course id

@mwingine
Copy link

mwingine commented Nov 4, 2020

@lucanos Thank you so much for this great code!! This was a hell to find, but eternally grateful.

@marsvieyra
If you want to integrate it with Elementor, you can do it multiple ways.
I changed the snippet a bit to work for me, on line 69 I changed it simply from:
return '<a href="'.$url.'" class="'.$class.'">'.$content.'</a>';
to:
return ''.$url.'';
Then in Elementor, I choose an element (in my case, an icon). As link for the icon, I choose "Dynamic tag" and then "Shortcode".

image
Put in the shortcode [ld_next_lesson_link] and the icon now get the dynamic link for starting the first lesson or resuming your lesson.

@Berand-cod
Copy link

Berand-cod commented May 25, 2021

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.

@ragster200
Copy 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'?

@jaimon82
Copy link

Thank you very much! It solves perfectly my problem!
:)

@VegasWebsiteDesigns
Copy link

VegasWebsiteDesigns commented Dec 21, 2021

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