LearnDash: "My Courses" List With Progress
Adds a [wpide_ld_my_courses] shortcode that lists every course the student is enrolled in, with their progress and a Start, Continue or Review link.
Opens WPIDE on
Code
if ( ! defined( 'LEARNDASH_VERSION' ) ) {
return;
}
add_shortcode( 'wpide_ld_my_courses', function () {
$user_id = get_current_user_id();
if ( ! $user_id ) {
return '<p>Please <a href="' . esc_url( wp_login_url( get_permalink() ) ) . '">log in</a> to see your courses.</p>';
}
$course_ids = learndash_user_get_enrolled_courses( $user_id );
if ( ! $course_ids ) {
return '<p>You are not enrolled in any courses yet.</p>';
}
$courses = get_posts( array( 'post_type' => 'sfwd-courses', 'post__in' => array_map( 'intval', $course_ids ), 'numberposts' => count( $course_ids ), 'orderby' => 'title', 'order' => 'ASC', 'no_found_rows' => true ) );
$html = '<ul class="wpide-ld-my-courses" style="list-style:none;padding:0">';
foreach ( $courses as $course ) {
$progress = learndash_course_progress( array( 'user_id' => $user_id, 'course_id' => $course->ID, 'array' => true ) );
$percent = (int) ( $progress['percentage'] ?? 0 );
$action = $percent >= 100 ? 'Review' : ( $percent > 0 ? 'Continue' : 'Start' );
$html .= sprintf(
'<li style="display:flex;flex-wrap:wrap;justify-content:space-between;gap:.5em;padding:.75em 0;border-bottom:1px solid rgba(127,127,127,.25)"><span><strong>%1$s</strong> <small>%2$d%% complete</small></span><a href="%3$s">%4$s</a></li>',
esc_html( get_the_title( $course ) ),
$percent,
esc_url( get_permalink( $course ) ),
esc_html( $action )
);
}
return $html . '</ul>';
} );
Notes
Visitors who are not logged in see a login link. Courses are listed in alphabetical order.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.