learndash-course-content-list-before
Fires before the course content listing Fires before the course content list is displayed, providing access to the current course and user IDs.
add_action( 'learndash-course-content-list-before', $callback, 10, 2 );
Description
Fires just before the course content list is rendered on the single course page. Developers can use this hook to conditionally modify or display content preceding the lessons, topics, and quizzes for a specific course and user.
Usage
add_action( 'learndash-course-content-list-before', 'your_function_name', 10, 2 );
Parameters
-
$course_id(int) - Course ID.
-
$user_id(int) - User ID.
Examples
add_action( 'learndash-course-content-list-before', 'my_learndash_course_content_list_before_handler', 10, 2 );
/**
* Example function to hook into 'learndash-course-content-list-before'.
* This function demonstrates adding a custom message or modifying the display
* based on course and user context before the course content is listed.
*
* @param int $course_id The ID of the current LearnDash course.
* @param int $user_id The ID of the current WordPress user.
*/
function my_learndash_course_content_list_before_handler( $course_id, $user_id ) {
// Check if the user has access to this course (example logic)
if ( ! sfwd_lms_has_access( $course_id, $user_id ) ) {
// If the user does not have access, display a message.
echo '<div class="learndash-no-access-message">';
echo '<p>' . sprintf( esc_html__( 'You do not currently have access to the content of course "%s". Please enroll to begin.', 'my-text-domain' ), get_the_title( $course_id ) ) . '</p>';
echo '</div>';
} else {
// If the user has access, perhaps display a welcome message or a progress summary.
// For demonstration, let's get the user's progress on the course.
$course_progress = learndash_get_course_progress( $course_id, $user_id );
if ( ! empty( $course_progress ) && $course_progress['percentage'] > 0 ) {
echo '<div class="learndash-course-progress-summary">';
echo '<p>' . sprintf( esc_html__( 'Welcome back, %s! You are %d%% complete with this course.', 'my-text-domain' ), esc_html( learndash_get_user_display_name( $user_id ) ), intval( $course_progress['percentage'] ) ) . '</p>';
echo '</div>';
} else {
echo '<div class="learndash-course-welcome">';
echo '<p>' . sprintf( esc_html__( 'Welcome to the course, %s! Let's get started.', 'my-text-domain' ), esc_html( learndash_get_user_display_name( $user_id ) ) ) . '</p>';
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-course.php:232
* Fires before the course content listing
*
* @since 3.0.0
*
* @param int $course_id Course ID.
* @param int $user_id User ID.
*/
do_action( 'learndash-course-content-list-before', $course_id, $user_id );
/**
* Content content listing
*
* @since 3.0
*
* ('listing.php');