Action Since 3.0 uncanny-toolkit-pro

learndash-lesson-assignment-before

Action to add custom content before the lesson assignment Fires before the lesson assignment is displayed, providing access to lesson and course IDs and the current user.

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

Description

Fires before the lesson assignment list is displayed for a logged-in user. Developers can use this action to insert custom content, messages, or logic. It passes the current lesson ID, course ID, and user ID as arguments.


Usage

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

Parameters

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

Examples

add_action( 'learndash-lesson-assignment-before', 'my_learndash_lesson_assignment_before_content', 10, 3 );

/**
 * Example function to display custom content before the LearnDash lesson assignment listing.
 * This could be used to show specific instructions or supplementary information related to assignments for a particular 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_learndash_lesson_assignment_before_content( $lesson_id, $course_id, $user_id ) {
    // Check if the current user is enrolled in the course and has permission to see assignments.
    // In a real-world scenario, you might have more complex checks here.
    if ( ! learndash_is_user_enrolled( $course_id, $user_id ) ) {
        return; // Don't display custom content if the user is not enrolled.
    }

    // Example: Display a unique message if the lesson is a specific assignment lesson.
    // You might check for custom meta, specific lesson types, or other conditions.
    $lesson_title = get_the_title( $lesson_id );
    $course_title = get_the_title( $course_id );

    echo '<div class="my-custom-assignment-notice">';
    echo sprintf(
        esc_html__( 'Before you submit your assignment for "%s" in the course "%s", please review the following important guidelines:', 'your-text-domain' ),
        $lesson_title,
        $course_title
    );
    echo '</div>';

    // Example: Conditionally display additional resources based on lesson or course.
    if ( $lesson_id === 123 ) { // Replace 123 with an actual lesson ID
        echo '<div class="additional-resources">';
        echo '<h3>' . esc_html__( 'Additional Resources:', 'your-text-domain' ) . '</h3>';
        echo '<ul>';
        echo '<li><a href="' . esc_url( 'https://example.com/assignment-guide-lesson-123' ) . '" target="_blank">' . esc_html__( 'Lesson 123 Assignment Guide', 'your-text-domain' ) . '</a></li>';
        echo '</ul>';
        echo '</div>';
    }
}

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:110

if ( $assignments_exist_fn( $post ) && ! empty( $user_id ) ) :

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

			learndash_get_template_part(
				'assignment/listing.php',
				array(
					'course_step_post' => $post,
					'user_id'          => $user_id,
					'course_id'        => $course_id,


Scroll to Top