Action Since 3.0.0 uncanny-toolkit-pro

learndash-course-after

Fires before the topic. Fires after a LearnDash course has been processed, providing course, post, and user IDs.

add_action( 'learndash-course-after', $callback, 10, 3 );

Description

Fires after the main course content and before the login modal on the single course page. This hook is useful for adding custom elements or modifying the output after the course content has been displayed. It provides the current post ID, course ID, and user ID as parameters.


Usage

add_action( 'learndash-course-after', 'your_function_name', 10, 3 );

Parameters

$post_id (int)
Post ID.
$course_id (int)
Course ID.
$user_id (int)
User ID.

Examples

/**
 * Example function to hook into the 'learndash-course-after' action.
 * This function demonstrates how to access the course post ID, course ID, and user ID.
 *
 * @param int $post_id   The ID of the current post being displayed (which is the course post).
 * @param int $course_id The ID of the LearnDash course.
 * @param int $user_id   The ID of the current logged-in user.
 */
function my_learndash_course_after_content( $post_id, $course_id, $user_id ) {

	// Check if the user is logged in before proceeding with user-specific logic.
	if ( $user_id && get_userdata( $user_id ) ) {

		// Example: Log a message indicating the course and user.
		error_log( sprintf( 'LearnDash course "%s" (ID: %d) has finished rendering for user "%s" (ID: %d).', get_the_title( $post_id ), $course_id, get_user_meta( $user_id, 'display_name', true ), $user_id ) );

		// Example: Check if the user has started or completed the course and add a class to the body.
		if ( function_exists( 'ld_course_status' ) ) {
			$course_status = ld_course_status( $course_id, $user_id );

			if ( 'completed' === $course_status ) {
				add_action( 'wp_body_open', function() use ( $post_id ) {
					echo '<body class="learndash-course-completed-' . esc_attr( $post_id ) . '">';
				} );
			} elseif ( 'in_progress' === $course_status ) {
				add_action( 'wp_body_open', function() use ( $post_id ) {
					echo '<body class="learndash-course-in-progress-' . esc_attr( $post_id ) . '">';
				} );
			}
		}

		// Example: Display a custom message to the user if they have completed the course.
		if ( function_exists( 'ld_course_status' ) && 'completed' === ld_course_status( $course_id, $user_id ) ) {
			add_action( 'learndash-course-after', function( $p_id, $c_id, $u_id ) {
				if ( $p_id === get_the_ID() ) {
					echo '<div class="my-custom-completion-message" style="background-color: #e0ffe0; padding: 15px; margin-top: 20px; border-left: 5px solid #4CAF50;">Congratulations on completing this course!</div>';
				}
			}, 10, 3 );
		}

	} else {
		// Logic for when the user is not logged in.
		error_log( sprintf( 'LearnDash course "%s" (ID: %d) has finished rendering for a guest user.', get_the_title( $post_id ), $course_id ) );
	}
}
add_action( 'learndash-course-after', 'my_learndash_course_after_content', 10, 3 );

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-course.php:317

*
	 * @since 3.0.0
	 *
	 * @param int $post_id   Post ID.
	 * @param int $course_id Course ID.
	 * @param int $user_id   User ID.
	 */
	do_action( 'learndash-course-after', get_the_ID(), $course_id, $user_id );
	learndash_load_login_modal_html();
	?>
</div>


Scroll to Top