Action Since 3.0.0 uncanny-toolkit-pro

learndash-course-before

Fires before the topic. Fires before a LearnDash course starts, providing access to course, topic, and user IDs.

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

Description

Fires before the course content in the LearnDash course template. Developers can hook into this action to add custom content or modify the course display before the main course structure is rendered. It provides the current post ID, course ID, and user ID as parameters.


Usage

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

Parameters

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

Examples

<?php
/**
 * Example function to hook into learndash-course-before.
 * This function logs some information when a course page is about to be displayed.
 *
 * @param int $post_id   The ID of the current post (likely the course post).
 * @param int $course_id The ID of the LearnDash course.
 * @param int $user_id   The ID of the currently logged-in user.
 */
function my_learndash_course_before_logger( $post_id, $course_id, $user_id ) {
    // Check if the user is logged in and has access to the course.
    if ( $user_id && learndash_is_user_enrolled( $course_id, $user_id ) ) {
        $user_info = get_userdata( $user_id );
        $course_title = get_the_title( $course_id );

        // Log a message to the debug log (or another logging mechanism).
        // Make sure WP_DEBUG and WP_DEBUG_LOG are enabled in wp-config.php for this to work.
        error_log(
            sprintf(
                'LearnDash Course Before Hook: User "%s" (ID: %d) is about to view Course "%s" (ID: %d) on Post ID: %d.',
                $user_info->user_login,
                $user_id,
                $course_title,
                $course_id,
                $post_id
            )
        );
    } else {
        // Log for guests or users not enrolled.
        $post_title = get_the_title( $post_id );
        error_log(
            sprintf(
                'LearnDash Course Before Hook: Guest or non-enrolled user viewing Course "%s" (ID: %d) on Post ID: %d.',
                $post_title,
                $course_id,
                $post_id
            )
        );
    }
}
add_action( 'learndash-course-before', 'my_learndash_course_before_logger', 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:53

*
	 * @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-before', get_the_ID(), $course_id, $user_id );

	/**
	 * Fires before the course certificate link.
	 *
	 * @since 3.0.0
	 *
	 * @param int $course_id Course ID.


Scroll to Top