Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.