learndash-lesson-before
Action to add custom content before the lesson Fires before a LearnDash lesson's content, providing lesson, course, and user IDs for custom modifications.
add_action( 'learndash-lesson-before', $callback, 10, 1 );
Description
Fires before LearnDash lesson content displays. Use this hook to inject custom elements, modify the lesson context, or perform actions prior to rendering the lesson for a specific user and course. It receives the current lesson ID, course ID, and user ID as parameters.
Usage
add_action( 'learndash-lesson-before', 'your_function_name', 10, 1 );
Parameters
-
$course_id(mixed) - - **$user_id** `mixed`
Examples
<?php
/**
* Example function to display a notice before the LearnDash lesson content.
*
* This function checks if the current user has access to the lesson.
* If they don't, it displays a custom message.
*/
function my_learndash_lesson_before_notice( $lesson_id, $course_id, $user_id ) {
// Check if the user has access to this specific lesson within the course.
// Replace with your actual access checking logic if needed.
// For this example, we'll assume a simplified check or rely on LearnDash's internal checks.
if ( ! sfwd_lms_has_access( $course_id, $user_id ) || ! sfwd_lms_has_access( $lesson_id, $user_id ) ) {
?>
<div class="my-custom-lesson-notice">
<p><strong>Important Note:</strong> You do not currently have access to this lesson. Please ensure you are enrolled in the course.</p>
</div>
<?php
}
// You could also perform other actions here, like logging or custom data retrieval.
// For example, logging the lesson and user ID:
// error_log( "Attempting to load lesson ID: " . $lesson_id . " for user ID: " . $user_id );
}
add_action( 'learndash-lesson-before', 'my_learndash_lesson_before_notice', 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-lesson.php:47
<?php
/**
* Action to add custom content before the lesson
*
* @since 3.0
*/
do_action( 'learndash-lesson-before', get_the_ID(), $course_id, $user_id );
learndash_get_template_part(
'modules/infobar.php',
array(
'context' => 'lesson',
'has_access' => $has_access,
'course_id' => $course_id,