Snippet: Add LearnDash Course Progress to the WordPress Users REST API
Attach a learndash_progress field to the core /wp/v2/users responses with register_rest_field(), containing each enrolled course and its percentage, limited to users who may see it and cheap enough for the users list.

Tools that already talk to the WordPress users endpoint, such as a CRM sync, a headless front end or an internal admin app, often want one more thing from each user: how far they are through their courses. Rather than teaching those tools a LearnDash route, you can add the data to the response they already receive. WordPress's register_rest_field() attaches a field to any object type, including user, and this snippet uses it in LearnDash 4.18.0.1 to add a learndash_progress array to /wp/v2/users and /wp/v2/users/me, with a schema, access rules and caching that keep the users list fast.
What the field contains
"learndash_progress": {
"courses": [
{ "id": 45, "title": "Data Protection Basics", "percentage": 100, "completed": 12, "total": 12, "completed_on": 1760992000 },
{ "id": 72, "title": "Manager Onboarding", "percentage": 25, "completed": 3, "total": 12, "completed_on": 0 }
],
"enrolled": 2,
"completed": 1
}
The two counts let a list view show "1 of 2 courses complete" without iterating, and the per-course array serves detail screens.
The code
<?php
add_action( 'rest_api_init', 'wwc_register_user_progress_field' );
function wwc_register_user_progress_field() {
register_rest_field(
'user',
'learndash_progress',
array(
'get_callback' => 'wwc_user_progress_field',
'schema' => array(
'description' => __( 'LearnDash course progress for the user.', 'wwc' ),
'type' => 'object',
'context' => array( 'view', 'edit' ),
'readonly' => true,
'properties' => array(
'courses' => array( 'type' => 'array' ),
'enrolled' => array( 'type' => 'integer' ),
'completed' => array( 'type' => 'integer' ),
),
),
)
);
}
/**
* Field value. Returns null when the requester may not see this user's progress.
*/
function wwc_user_progress_field( $user, $field_name, WP_REST_Request $request ) {
$user_id = (int) $user['id'];
if ( ! wwc_user_progress_visible( $user_id ) ) {
return null;
}
$cache_key = 'wwc_user_progress_' . $user_id;
$data = get_transient( $cache_key );
if ( false !== $data ) {
return $data;
}
$courses = array();
$completed = 0;
foreach ( learndash_user_get_enrolled_courses( $user_id ) as $course_id ) {
$progress = learndash_course_progress(
array(
'user_id' => $user_id,
'course_id' => $course_id,
'array' => true,
)
);
$completed_on = (int) learndash_user_get_course_completed_date( $user_id, $course_id );
$steps_done = (int) ( $progress['completed'] ?? 0 );
$steps_total = (int) ( $progress['total'] ?? 0 );
if ( $completed_on ) {
$completed++;
}
$courses[] = array(
'id' => (int) $course_id,
'title' => get_the_title( $course_id ),
'percentage' => (int) ( $progress['percentage'] ?? 0 ),
'completed' => $steps_done,
'total' => $steps_total,
'completed_on' => $completed_on,
);
}
$data = array(
'courses' => $courses,
'enrolled' => count( $courses ),
'completed' => $completed,
);
set_transient( $cache_key, $data, 5 * MINUTE_IN_SECONDS );
return $data;
}
/**
* Same visibility rules LearnDash applies to its progress endpoints.
*/
function wwc_user_progress_visible( $user_id ) {
$current = get_current_user_id();
if ( ! $current ) {
return false;
}
if ( $current === $user_id || learndash_is_admin_user( $current ) ) {
return true;
}
return learndash_is_group_leader_user( $current ) && learndash_is_group_leader_of_user( $current, $user_id );
}
/**
* Keep the cache fresh when progress or enrollment changes.
*/
function wwc_user_progress_flush( $data_or_user ) {
$user_id = is_array( $data_or_user ) ? absint( $data_or_user['user']->ID ?? 0 ) : absint( $data_or_user );
if ( $user_id ) {
delete_transient( 'wwc_user_progress_' . $user_id );
}
}
add_action( 'learndash_course_completed', 'wwc_user_progress_flush' );
add_action( 'learndash_lesson_completed', 'wwc_user_progress_flush' );
add_action( 'learndash_topic_completed', 'wwc_user_progress_flush' );
add_action( 'learndash_update_course_access', 'wwc_user_progress_flush' );
Why these choices
Visibility is enforced inside the callback. register_rest_field() has no permission callback of its own; the core users endpoint decides who may see a user record, and that is broader than who may see their learning data. For example, authenticated users with the right capability can list users, and public user listings may be enabled on some sites. Returning null for anyone who is not the user, an administrator or one of their group leaders applies the same rules the LearnDash progress controllers use, so the field never leaks more than the official endpoints would.
Caching per user. The users list returns ten users by default and up to a hundred. Computing progress for a hundred users means a hundred enrolled-course lookups and one progress calculation per course, which is too slow for a list. A five-minute transient per user keeps the list quick, and the completion and enrollment hooks delete the transient the moment something changes, so the data is fresh when it matters. The completion hooks pass an array containing the user object, while the enrollment hook passes the user ID first; the flush function accepts both.
No side effects. learndash_course_progress() in array mode reads stored progress and never marks anything complete, which is essential in a callback that runs on every user request.
Requesting the field
GET /wp-json/wp/v2/users/me?_fields=id,name,learndash_progress
GET /wp-json/wp/v2/users?context=edit&per_page=50&_fields=id,email,learndash_progress
The _fields parameter matters more than usual here: when a client asks only for other fields, WordPress skips the callback entirely, so clients that do not need progress pay nothing for the field's existence. Note that the users collection with context=edit requires the list_users capability, which is the normal route for an admin tool; a learner can read their own record through /users/me.
Compared with the LearnDash endpoints and a custom route
LearnDash's own users/{id}/course-progress route returns richer data per course but needs one call per user and per course. A custom route as in our custom progress endpoint snippet gives one call per user with a shape you control. The REST field is the right choice when the client already consumes /wp/v2/users and you want to avoid a second request per user; it is the wrong choice for very large user lists without the caching above. For other ways of extending LearnDash responses, see adding custom fields to LearnDash REST responses.
Limiting the field to the edit context
If the field should exist only for administrative tools and never appear in the public view context, change the schema's context to array( 'edit' ). WordPress then omits the field from responses requested without context=edit, and since that context already requires the list_users capability on the users collection, the visibility check inside the callback becomes a second line of defence rather than the first. Leave view in place when learners should see their own progress through /users/me, which is requested in the view context by default.
What the field cannot do
A REST field adds data to each returned user; it cannot filter or sort the users collection. "Give me all users below 50% in course 45" is not possible through /wp/v2/users with this snippet, because the collection's filtering runs in SQL before fields are computed. For that question, write a custom route that queries progress first and returns user IDs, as described in the custom controller guide, or use the activity table directly. Likewise, the field is computed per user, so a report across thousands of learners should be generated server-side on a schedule rather than paged through the API.
Choosing the cache lifetime
Five minutes suits a CRM sync that runs hourly and a dashboard that refreshes on load. A live classroom screen that polls every few seconds should use a shorter lifetime or rely entirely on the flush hooks, while a nightly warehouse export can use an hour. Whatever the value, keep the flush hooks in place; they are what make a cache acceptable for data learners can see about themselves.
Testing
Request /users/me as a learner and confirm the field lists their courses with the same percentages as their profile page. Request the users collection as an administrator with context=edit and confirm each row has the field, then as a group leader and confirm non-members show null. Mark a lesson complete, request again within five minutes and confirm the percentage changed, which proves the flush hooks fire. Finally, time the collection request for a hundred users with a warm cache; it should be close to the time without the field.
When to get help
The field is an hour's work. Syncing LearnDash progress into a CRM or data warehouse continuously, with retries and change detection, is an integration project; it is routine for our LearnDash services and API integration teams, and a LearnDash developer can also tune the caching for sites with tens of thousands of learners.
Frequently asked questions
Can anyone see a user's LearnDash progress through this field?
No. The callback returns null unless the requester is that user, an administrator or one of their group leaders, matching LearnDash's own progress endpoints.
Will this slow down the users endpoint?
Not noticeably with the per-user transient, and not at all for clients that use _fields to exclude the field.
Why does the field show stale data for a few minutes?
The transient lasts five minutes, but completion and enrollment hooks delete it immediately. Stale data means a change happened through a path that fires neither hook, such as a direct database edit.
Can I make the field writable to set progress?
You could add an update_callback, but setting progress through the users endpoint is a poor fit. Use LearnDash's course-progress endpoints or a dedicated route with explicit permissions instead.
Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.
