LMS & eLearning

Add an Enrolled Courses Column to the WordPress Users List

Show each user's LearnDash course count and titles directly in Users > All Users with manage_users_columns and learndash_user_get_enrolled_courses(), plus sorting notes and a per-user cache so the list stays fast.

Add an Enrolled Courses Column to the WordPress Users List

Support staff answer "which courses does this person have?" dozens of times a week, and the answer is hidden behind the user's edit screen and the LearnDash Course Info tab. A column in Users, All Users, that lists the enrolled courses turns that into a glance. This article adds the column with two core WordPress filters and one LearnDash 4.18.0.1 function, explains what the function counts, and adds a cache so a list of twenty users does not run twenty expensive lookups.

What counts as enrolled

learndash_user_get_enrolled_courses( $user_id, $course_query_args = array(), $bypass_transient = false ) in includes/ld-users.php returns an array of course IDs. For users who can auto-enroll in everything, which learndash_can_user_autoenroll_courses() decides and which includes administrators by default, it returns all courses. For everyone else it merges open courses, direct enrollments and courses granted through groups, and removes duplicates. Two points matter for the column:

  • Open courses count for every user, so a site with several open courses shows them for everyone. If that is noise, filter them out by price type as shown below.
  • The function's transient is effectively bypassed in this version: the code sets the bypass flag to true before checking it, so each call does the full work. That is why the snippet adds its own short cache.

The snippet

<?php
/**
 * Add a LearnDash courses column to the users list.
 */
add_filter(
    'manage_users_columns',
    function ( $columns ) {
        $columns['wwc_ld_courses'] = sprintf(
            /* translators: %s: Courses label */
            __( 'Enrolled %s', 'wwc' ),
            LearnDash_Custom_Label::get_label( 'courses' )
        );
        return $columns;
    }
);

add_filter(
    'manage_users_custom_column',
    function ( $output, $column_name, $user_id ) {
        if ( 'wwc_ld_courses' !== $column_name ) {
            return $output;
        }

        $course_ids = wwc_get_user_course_ids_cached( (int) $user_id );

        if ( empty( $course_ids ) ) {
            return '&mdash;';
        }

        $titles = array();
        foreach ( array_slice( $course_ids, 0, 3 ) as $course_id ) {
            $titles[] = esc_html( get_the_title( $course_id ) );
        }

        $more = count( $course_ids ) - count( $titles );
        $text = implode( ', ', $titles );

        if ( $more > 0 ) {
            /* translators: %d: number of additional courses */
            $text .= ' ' . esc_html( sprintf( __( 'and %d more', 'wwc' ), $more ) );
        }

        return '<strong>' . (int) count( $course_ids ) . '</strong><br>' . $text;
    },
    10,
    3
);

function wwc_get_user_course_ids_cached( $user_id ) {
    $key = 'wwc_user_courses_' . $user_id;
    $ids = get_transient( $key );

    if ( false !== $ids ) {
        return (array) $ids;
    }

    $ids = learndash_user_get_enrolled_courses( $user_id );

    // Leave open courses out of the column; they are not real enrollments.
    $ids = array_filter(
        (array) $ids,
        function ( $course_id ) {
            return 'open' !== learndash_get_setting( $course_id, 'course_price_type' );
        }
    );

    set_transient( $key, array_values( $ids ), 15 * MINUTE_IN_SECONDS );

    return array_values( $ids );
}

add_action(
    'learndash_update_course_access',
    function ( $user_id ) {
        delete_transient( 'wwc_user_courses_' . absint( $user_id ) );
    }
);

The custom column filter returns HTML, so each piece is escaped as it is built. The count is bold on the first line and up to three titles follow, which keeps the row height reasonable on sites where learners hold many courses. The cache is cleared on learndash_update_course_access, which fires for direct enrollments and removals; group changes refresh within fifteen minutes.

Linking to the course info tab

Make the count a link to the user's edit screen, where LearnDash's Course Info section lists progress per course. Build the URL with get_edit_user_link( $user_id ) and append it to the anchor's attributes, escaping with esc_url(). For group leaders, who may not be able to edit users, point the link at the group's report instead or leave the count plain.

Sorting

WordPress can only sort the users table by a database column or a single user meta key. Enrollment is spread across several meta keys and group membership, so there is nothing to sort on directly. If sorting matters, maintain a wwc_course_count user meta value, updated on the same learndash_update_course_access hook and the group hooks ld_added_group_access and ld_removed_group_access, then register the column as sortable with manage_users_sortable_columns and add meta_key and orderby to the user query in pre_get_users. The maintenance is small once the hooks are in place; the backfill for existing users is a one-off WP-CLI loop.

Filtering the list by course

A dropdown above the users table that filters to one course is a different problem and needs a user query that knows the course's enrolled IDs. learndash_get_users_for_course( $course_id ) returns those as a WP_User_Query object; take its results and pass them as include in pre_get_users when your dropdown value is set. On very large courses the include list becomes long, which is where the Group or Course filters on the LearnDash reports screens may serve better; our guide to adding columns to the LearnDash course and group lists covers the plugin's own listing filters.

Performance on large user tables

Each row costs one enrolled-courses call on a cache miss, which involves a handful of meta reads and group lookups. With the fifteen-minute transient and twenty users per page, the first load after a cache flush is the only slow one. If your site uses an object cache, transients live there and the cost disappears almost entirely. Avoid raising the users-per-page screen option to hundreds while the column is active.

Telling direct and group enrollments apart

Support often needs to know not just which courses a user has but why. A course granted through a group cannot be removed from the user's course list; it goes when the group membership goes. To show the distinction, fetch the user's group course IDs separately with learndash_get_user_groups_courses_ids( $user_id ) and mark those titles with a suffix such as "(via group)". The remaining IDs are direct or open enrollments. Because the enrolled-courses function already merges the two sets, the extra call costs one more cached lookup per row and saves a trip to the user's edit screen when someone asks why a course "will not unenroll".

Testing

  1. Enroll a test user in two courses directly and one through a group, and confirm the column shows three with the three titles.
  2. Remove one direct enrollment and confirm the column updates immediately, because the hook cleared the cache.
  3. Remove the group enrollment and confirm the column updates within fifteen minutes.
  4. Check an administrator row: it lists all non-open courses, because administrators auto-enroll.

When to get help

The column is an hour's work. A full enrollment management screen with filters, bulk enroll and unenroll, and export is a small admin plugin that our LearnDash services team builds, and a LearnDash developer can also add the sortable count meta with a backfill for existing learners.

Frequently asked questions

Which function lists a user's LearnDash courses?

learndash_user_get_enrolled_courses( $user_id ), which returns course IDs from direct, group and open enrollments, or all courses for users who auto-enroll.

Why do administrators show every course?

Administrators pass learndash_can_user_autoenroll_courses(), so the function returns all published courses for them.

Can I sort the users list by course count?

Not directly. Maintain a count in user meta on the enrollment hooks and make the column sortable on that meta key.

Does the column slow the users screen down?

Slightly on a cold cache; the transient keeps repeat loads fast. Keep the users-per-page option modest.

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.