LMS & eLearning

A Continue Learning Widget With learndash_get_last_active_course

Build a Continue Learning card for headers, sidebars and the home page that shows the learner's current course, progress and the exact step to resume, using learndash_get_last_active_course() and the step helpers.

A Continue Learning Widget With learndash_get_last_active_course

The fastest way to raise course completion on a LearnDash site is to make continuing take one click: wherever a learner is on the site, one click should take them back into the lesson they were on. LearnDash 4.18.0.1 has a resume shortcode that prints a bare button. This tutorial builds a fuller Continue Learning card with the course title, thumbnail, progress and the step name, packaged as a shortcode, a classic widget and a header menu link, using the same functions the resume shortcode relies on.

How LearnDash knows where a learner was

Two functions in core do the work. learndash_get_last_active_course( $user_id ) queries the user activity table for the user's course rows that are not completed, ordered by activity_updated, and returns the newest course ID, or 0. Because it ignores completed courses, a learner who just finished a course gets their next unfinished course instead, and someone who has finished everything gets 0. learndash_user_course_last_step( $user_id, $course_id ) then reads the steps_last_id value from that course's activity meta, which LearnDash updates as the learner moves through steps, and returns the step ID. If you pass no course ID it calls the first function itself. The resume shortcode, [ld_course_resume], combines the two and builds the link with learndash_get_step_permalink( $step_id, $course_id ), which produces the nested course URL when shared steps are enabled.

There is a second meaning of "continue" worth offering: the first step the learner has not completed, from learndash_user_progress_get_first_incomplete_step( $user_id, $course_id ). In linear courses the two usually match. In free-form courses, where learners jump around, the last visited step may be a lesson they finished while the first incomplete step is earlier. The card below uses the last visited step and falls back to the first incomplete one.

The card function

<?php
/**
 * Escaped anchor for the resume button.
 *
 * @param string $url   Link target.
 * @param string $text  Link text.
 * @param array  $attrs Extra attributes.
 * @return string
 */
function wwc_resume_anchor( $url, $text, $attrs = array() ) {
    $attrs     = array_merge( array( 'href' => esc_url( $url ) ), $attrs );
    $attr_html = '';
    foreach ( $attrs as $attr_name => $attr_value ) {
        $attr_html .= sprintf( ' %1$s="%2$s"', esc_attr( $attr_name ), esc_attr( $attr_value ) );
    }
    return '<a' . $attr_html . '>' . esc_html( $text ) . '</a>';
}

/**
 * Render a Continue Learning card for the current user.
 *
 * @param array $args { show_thumb: bool, label: string }
 * @return string
 */
function wwc_continue_learning_card( $args = array() ) {
    $args = wp_parse_args(
        $args,
        array(
            'show_thumb' => true,
            'label'      => __( 'Continue learning', 'wwc' ),
        )
    );

    $user_id = get_current_user_id();
    if ( ! $user_id ) {
        return '';
    }

    $course_id = learndash_get_last_active_course( $user_id );
    if ( ! $course_id || ! sfwd_lms_has_access( $course_id, $user_id ) ) {
        return '';
    }

    $step_id = learndash_user_course_last_step( $user_id, $course_id );
    if ( ! $step_id ) {
        $step_id = learndash_user_progress_get_first_incomplete_step( $user_id, $course_id );
    }

    $url      = $step_id ? learndash_get_step_permalink( $step_id, $course_id ) : get_permalink( $course_id );
    $progress = learndash_course_progress(
        array(
            'user_id'   => $user_id,
            'course_id' => $course_id,
            'array'     => true,
        )
    );
    $percent  = absint( $progress['percentage'] ?? 0 );

    ob_start();
    ?>
    <div class="wwc-continue learndash-wrapper">
        <?php if ( $args['show_thumb'] && has_post_thumbnail( $course_id ) ) : ?>
            <?php echo get_the_post_thumbnail( $course_id, 'thumbnail', array( 'class' => 'wwc-continue__thumb' ) ); ?>
        <?php endif; ?>
        <div class="wwc-continue__body">
            <p class="wwc-continue__label"><?php echo esc_html( $args['label'] ); ?></p>
            <p class="wwc-continue__course"><?php echo esc_html( get_the_title( $course_id ) ); ?></p>
            <?php if ( $step_id ) : ?>
                <p class="wwc-continue__step"><?php echo esc_html( get_the_title( $step_id ) ); ?></p>
            <?php endif; ?>
            <div class="wwc-continue__bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="<?php echo esc_attr( $percent ); ?>">
                <span style="width:<?php echo esc_attr( $percent ); ?>%"></span>
            </div>
            <?php echo wwc_resume_anchor( $url, __( 'Resume', 'wwc' ), array( 'class' => 'ld-button' ) ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
        </div>
    </div>
    <?php
    return ob_get_clean();
}

add_shortcode(
    'wwc_continue',
    function ( $atts ) {
        $atts = shortcode_atts( array( 'thumb' => 'yes', 'label' => '' ), $atts );
        return wwc_continue_learning_card(
            array(
                'show_thumb' => 'yes' === $atts['thumb'],
                'label'      => $atts['label'] ? sanitize_text_field( $atts['label'] ) : __( 'Continue learning', 'wwc' ),
            )
        );
    }
);

The access check matters. A learner whose access expired still has activity rows, so without sfwd_lms_has_access() the card would send them to a course they can no longer open. The card returns an empty string in every "nothing to show" case so you can place it anywhere without empty boxes.

As a classic widget

<?php
class WWC_Continue_Learning_Widget extends WP_Widget {
    public function __construct() {
        parent::__construct( 'wwc_continue_learning', __( 'Continue Learning', 'wwc' ) );
    }

    public function widget( $args, $instance ) {
        $card = wwc_continue_learning_card( array( 'show_thumb' => false ) );
        if ( '' === $card ) {
            return;
        }
        echo $args['before_widget'] . $card . $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput -- escaped in the card.
    }
}

add_action(
    'widgets_init',
    function () {
        register_widget( 'WWC_Continue_Learning_Widget' );
    }
);

Block themes can use the shortcode inside a Shortcode block in a template part instead, for example in the header or a sidebar.

As a header menu item

A small "Resume" link in the main navigation is often more effective than a card, because it is visible on every page. Add it with the wp_nav_menu_items filter for a specific menu location, building the URL with the same three functions and printing nothing when there is no active course. Keep the label short and consistent, since it appears on every page.

Styling

.wwc-continue { display: flex; gap: 1rem; align-items: center; padding: 1rem; border: 1px solid #e5e7eb; border-radius: 10px; }
.wwc-continue__thumb { width: 72px; height: 72px; object-fit: cover; border-radius: 8px; }
.wwc-continue__label { margin: 0; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; color: #6b7280; }
.wwc-continue__course { margin: 0.15rem 0 0; font-weight: 700; }
.wwc-continue__step { margin: 0 0 0.5rem; color: #4b5563; }
.wwc-continue__bar { height: 6px; background: #e5e7eb; border-radius: 6px; overflow: hidden; margin-bottom: 0.75rem; }
.wwc-continue__bar span { display: block; height: 100%; background: #1d4ed8; }

The button uses .ld-button, so it follows the LearnDash accent colour setting.

Caching and performance

The card runs two small queries against the activity table and one progress read per page view, which is cheap. The concern is page caching: a cached page containing one learner's card must never be served to another. Most cache plugins bypass logged-in users, which is sufficient. If yours caches logged-in pages, load the card through a REST request after page load instead of rendering it into the HTML; our custom progress endpoint snippet shows the permission pattern.

Redirecting learners to their last course right after login, rather than showing a card, is covered in redirecting users to their last active course. A full dashboard built around the same card is in building a custom learner dashboard. For the function reference behind "next" and "first incomplete" steps, see the next and previous steps guide.

Showing several courses

Learners enrolled in several courses at once may want a card for each in-progress course rather than only the latest. Loop over learndash_user_get_enrolled_courses( $user_id ), skip courses where learndash_course_progress() reports 0% or 100%, and render the card for each remaining course, passing the course ID into a variant of the function. Sort by the last step's activity so the most recent comes first, and cap the list at three to keep the widget compact. For anything longer, link to the dashboard's full in-progress list instead.

Accessibility notes

The progress element in the card uses role="progressbar" with minimum, maximum and current values, so screen readers announce the percentage. Keep the step name as text rather than putting it only in the button, and give the button a label that makes sense out of context, such as "Resume" followed by the course name in visually hidden text, because screen reader users often navigate by links alone.

Testing

As a new learner, confirm the card does not render. Enroll, open the second lesson, then visit the home page and confirm the card names that lesson and links to it. Complete the course and confirm the card moves to another unfinished course or disappears. Expire access on a test course and confirm the card stops pointing to it. Finally, enable shared course steps and check the link uses the nested course URL.

When to get help

The card is an hour's work. Personalised home pages with recommendations, streaks or upcoming live sessions are larger builds that our LearnDash services team handles, and a LearnDash developer can turn the card into a block with editor settings.

Frequently asked questions

What does learndash_get_last_active_course return?

The ID of the most recently updated course in the user's activity that is not completed, or 0 when there is none.

Why does the card send learners to a lesson they already finished?

It uses the last visited step. Swap the order so learndash_user_progress_get_first_incomplete_step() is tried first if you prefer the first unfinished step.

Can I use the built-in resume shortcode instead?

Yes. [ld_course_resume] prints a button to the same step with a configurable label and class. The card adds title, step name and progress around it.

Does the card work with shared course steps?

Yes. learndash_get_step_permalink() builds the nested course URL when shared steps are enabled.

Add WorldWin Coder to your preferred sources

Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.

· Developers and project managers

Articles written and reviewed by the developers and project managers at WorldWin Coder, who build and maintain websites, web applications and mobile apps on WordPress, Moodle, PHP, Laravel, CodeIgniter and Flutter.

Keep reading

Have something to build, fix or move?

Tell us where you are. A developer reads every inquiry, and you get next steps and a free quote within one business day.