learndash-lesson-can-complete
Filters whether a user can complete a LearnDash lesson, allowing custom completion logic before the lesson is marked complete.
add_filter( 'learndash-lesson-can-complete', $callback, 10, 2 );
Description
Allows developers to programmatically control whether a user can mark a lesson as complete. By default, it returns true if all quizzes in the lesson are completed, the user is logged in, and a course ID is present. Modify this filter to implement custom logic for lesson completion.
Usage
add_filter( 'learndash-lesson-can-complete', 'your_function_name', 10, 2 );
Parameters
-
$course_id(mixed) - This parameter contains the ID of the current lesson being viewed.
-
$user_id(mixed) - This parameter contains the ID of the course the lesson belongs to.
Return Value
The filtered value.
Examples
/**
* Example of how to use the 'learndash-lesson-can-complete' filter.
*
* This function checks if a lesson has any prerequisites and, if so,
* determines if the user can complete the lesson based on whether
* those prerequisites have been met.
*
* @param bool $can_complete Whether the user can complete the lesson (initially true).
* @param int $lesson_id The ID of the current lesson.
* @param int $course_id The ID of the course the lesson belongs to.
* @param int $user_id The ID of the current user.
* @return bool Returns false if prerequisites are not met, otherwise returns the original $can_complete value.
*/
function my_learndash_restrict_lesson_completion_based_on_prerequisites( $can_complete, $lesson_id, $course_id, $user_id ) {
// Check if the lesson has any prerequisites defined.
// This is a hypothetical check; you'd replace this with actual logic
// to fetch prerequisites for a lesson if your LMS or a plugin supports it.
$lesson_prerequisites = get_post_meta( $lesson_id, 'lesson_prerequisites', true );
// If there are prerequisites and the user hasn't met them.
if ( ! empty( $lesson_prerequisites ) && is_array( $lesson_prerequisites ) ) {
// Hypothetically check if user has completed all prerequisites.
// Replace this with your actual prerequisite checking logic.
$prerequisites_met = true; // Assume met for this example
foreach ( $lesson_prerequisites as $prerequisite_id ) {
// Example: Check if the prerequisite lesson/topic/course is completed by the user.
// You'll need to adapt this based on how prerequisites are stored and checked.
// For instance, you might use LearnDash's built-in functions or custom meta.
if ( ! learndash_is_lesson_completed( $prerequisite_id, $user_id ) ) { // Example function call
$prerequisites_met = false;
break;
}
}
// If prerequisites are not met, prevent completion.
if ( ! $prerequisites_met ) {
return false;
}
}
// If no prerequisites or they are met, return the original $can_complete value.
return $can_complete;
}
add_filter( 'learndash-lesson-can-complete', 'my_learndash_restrict_lesson_completion_based_on_prerequisites', 10, 4 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/templates/single-ld30-lesson.php:239
* Set a variable to switch the next button to complete button
*
* @var $can_complete [bool] - can the user complete this or not?
*/
$can_complete = false;
if ( $all_quizzes_completed && $logged_in && ! empty( $course_id ) ) :
$can_complete = apply_filters( 'learndash-lesson-can-complete', true, get_the_ID(), $course_id, $user_id );
endif;
learndash_get_template_part(
'modules/course-steps.php',
array(
'course_id' => $course_id,
'course_step_post' => $post,