learndash-lesson-assignment-after
Action to add custom content after the lesson assignment Fires after a lesson assignment is processed, providing lesson, course, and user IDs for custom content.
add_action( 'learndash-lesson-assignment-after', $callback, 10, 1 );
Description
Fires after the lesson assignment is displayed within the single lesson template. Developers can use this hook to inject custom content, modify the assignment area, or perform actions related to the assignment's presence on the page. It provides access to the current lesson ID, course ID, and user ID.
Usage
add_action( 'learndash-lesson-assignment-after', 'your_function_name', 10, 1 );
Parameters
-
$course_id(mixed) - - **$user_id** `mixed`
Examples
add_action(
'learndash-lesson-assignment-after',
'my_learndash_lesson_assignment_after_handler',
10,
3
);
/**
* Example handler for the learndash-lesson-assignment-after action hook.
* This function demonstrates how to access lesson, course, and user IDs
* to perform custom actions after a lesson assignment is displayed.
*
* @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.
*/
function my_learndash_lesson_assignment_after_handler( $lesson_id, $course_id, $user_id ) {
// Check if the current user has completed this lesson.
if ( learndash_is_lesson_complete( $lesson_id, $user_id ) ) {
// Get some details about the lesson.
$lesson_title = get_the_title( $lesson_id );
// Display a custom message or perform another action.
echo '<div class="custom-lesson-completion-message">';
echo sprintf(
'Congratulations, %s! You have successfully completed the lesson "%s" in course ID %d.',
esc_html( get_user_meta( $user_id, 'display_name', true ) ),
esc_html( $lesson_title ),
absint( $course_id )
);
echo '</div>';
// You could also log this event, send a notification, or trigger other integrations.
// error_log( "User {$user_id} completed lesson {$lesson_id} in course {$course_id}." );
}
}
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:127
);
/**
* Action to add custom content after the lesson assignment
*
* @since 3.0
*/
do_action( 'learndash-lesson-assignment-after', get_the_ID(), $course_id, $user_id );
endif;
/**
* Lesson Topics or Quizzes
*/
if ( ! empty( $topics ) || ! empty( $quizzes ) ) :