Action Since 3.0 uncanny-toolkit-pro

learndash-lesson-content-list-before

Action to add custom content before the course certificate link Fires before the LearnDash lesson content list displays, allowing custom content insertion.

add_action( 'learndash-lesson-content-list-before', $callback, 10, 1 );

Description

Fires before the list of lesson topics and quizzes. Developers can use this action hook to inject custom content or modify the display of the lesson content list. It's triggered after retrieving lesson and course data but before rendering the actual topic and quiz elements.


Usage

add_action( 'learndash-lesson-content-list-before', 'your_function_name', 10, 1 );

Parameters

$course_id (mixed)
- **$user_id** `mixed`

Examples

/**
 * Example function to display a notice before the lesson content list.
 *
 * This function demonstrates how to hook into the 'learndash-lesson-content-list-before'
 * action to display custom content before the list of topics or quizzes within a LearnDash lesson.
 *
 * @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 currently logged-in user.
 */
function my_custom_learndash_lesson_notice( $lesson_id, $course_id, $user_id ) {
    // Only display the notice if the user is enrolled in the course.
    if ( ! sfwd_is_user_enrolled( $course_id, $user_id ) ) {
        return;
    }

    // Get the lesson object to check its title.
    $lesson = get_post( $lesson_id );

    // Display a custom message if the lesson title contains "Introduction".
    if ( strpos( $lesson->post_title, 'Introduction' ) !== false ) {
        echo '<div class="learndash-lesson-notice notice notice-info">';
        echo '<p>Welcome to the introduction of this lesson! Take your time to understand the basics.</p>';
        echo '</div>';
    }
}
add_action( 'learndash-lesson-content-list-before', 'my_custom_learndash_lesson_notice', 10, 3 );

/**
 * Example function to conditionally hide topic list based on user role.
 *
 * This function demonstrates how to hook into the 'learndash-lesson-content-list-before'
 * action to conditionally hide the lesson content list for specific user roles.
 *
 * @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 currently logged-in user.
 */
function my_hide_lesson_content_for_certain_roles( $lesson_id, $course_id, $user_id ) {
    $current_user = wp_get_current_user();
    $restricted_roles = array( 'contributor', 'author' ); // Example roles to restrict

    // Check if the current user has any of the restricted roles.
    if ( array_intersect( $restricted_roles, $current_user->roles ) ) {
        // If restricted, prevent the display of further lesson content by clearing the output buffer.
        // This is a more aggressive approach and might need careful consideration.
        // A better approach might be to modify the logic that displays the list itself.
        // For demonstration purposes, we'll just output a message.
        echo '<div class="learndash-lesson-restricted-message">';
        echo '<p>Content for this lesson is not available to your current role.</p>';
        echo '</div>';
        // In a real-world scenario, you might want to enqueue a script here to hide
        // the subsequent elements using CSS or JavaScript based on this hook's output.
    }
}
// This hook should ideally be used to *add* content, not to conditionally *remove* it by clearing buffers.
// The following is a conceptual example and might not be the best practice for hiding content.
// add_action( 'learndash-lesson-content-list-before', 'my_hide_lesson_content_for_certain_roles', 20, 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-lesson.php:141

if ( ! empty( $topics ) || ! empty( $quizzes ) ) :

			/**
			 * Action to add custom content before the course certificate link
			 *
			 * @since 3.0
			 */
			do_action( 'learndash-lesson-content-list-before', get_the_ID(), $course_id, $user_id );

			global $post;
			$lesson = array(
				'post' => $post,
			);

			if ( ! has_shortcode( $post->post_content, 'uo_lessons_topics_grid' ) ) {


Scroll to Top