Learndash course access

From Lonsdale201/wp-agent-skills · View interactive version

Learndash course access is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Build or audit LearnDash course access and enrollment logic. Use when code mentions sfwd_lms_has_access, ld_update_course_access, ld_course_access_from, ld_course_access_from_update, ld_course_access_expired, learndash_user_get_enrolled_courses, course_*_access_from, open/free/paynow/closed course p

How to use this skill

Skill file

LearnDash course access

Use this when a plugin, theme, migration, checkout bridge, import, or custom

frontend needs to decide whether a user can access a LearnDash course or must

grant/revoke direct course enrollment.

Do not reduce LearnDash access to one usermeta key. In LearnDash 5.1.9, course

access can come from direct enrollment, group enrollment, open courses, sample

steps, admin auto-enroll, free/paynow settings, start/end dates, expiration

rules, and legacy access-list mode.

When to use this skill

Trigger when ANY of these appears:

ld_course_access_from(), ld_course_access_from_update(),

ld_course_access_expired(), learndash_get_users_for_course(), or

learndash_user_get_enrolled_courses().

learndash_course_123_enrolled_at, or learndash_course_expired_123.

bridge, or REST endpoint that grants/removes course access.

see course", "expired users still appear", or "open course behaves weirdly".

Access decision chain

sfwd_lms_has_access( $post_id, $user_id ) is the canonical yes/no gate.

It resolves the course for the passed post ID and then applies these broad

rules:

  1. No related course can mean access allowed for non-course content.
  2. Missing course post/status means no access.
  3. Admin auto-enroll, sample steps, and open courses can grant access without

usermeta enrollment.

  1. Some free/paynow configurations can grant access without an explicit user.
  2. Direct access uses course_{$course_id}_access_from usermeta.
  3. Group access is checked through learndash_user_group_enrolled_to_course().
  4. Direct non-group access also respects the Product model start/end window.
  5. Final access can be revoked by ld_course_access_expired().
  6. The result is filterable through sfwd_lms_has_access.

If the task is authorization, call the access API. Do not query only

course_{$course_id}_access_from, because group and open-course access will be

missed.

Grant and revoke correctly

Modern LearnDash exposes LearnDash\Core\Models\Product for course/group

products. It delegates to the public access helpers and applies product-level

filters.

use LearnDash\Core\Models\Product;

$product = Product::find( $course_id );

if ( $product ) {
    $product->enroll( $user_id );
    // $product->unenroll( $user_id );
}

The lower-level public helper is still the important compatibility surface:

ld_update_course_access( $user_id, $course_id );        // grant
ld_update_course_access( $user_id, $course_id, true );  // revoke

ld_update_course_access() does more than write usermeta:

enrollment time.

learndash_course_{$course_id}_enrolled_at, because reports use it.

Use ld_course_access_from_update( $course_id, $user_id, $timestamp ) when the

task is changing the access start timestamp. It updates both access activity and

the direct access usermeta. Do not write that meta directly unless you are doing

a one-off repair and also handle cache/activity consequences.

Read access state

Use the smallest API that matches the question:

sfwd_lms_has_access( $post_id, $user_id ).

ld_course_access_from( $course_id, $user_id ).

ld_course_access_expires_on( $course_id, $user_id ).

learndash_user_get_enrolled_courses( $user_id ).

learndash_get_users_for_course( $course_id ).

Important query caveats:

unless admin users are excluded by the query settings.

legacy access-list courses, direct access meta, and group courses.

learndash_user_get_enrolled_courses() is effectively forced to true before

the function rebuilds the list. Do not build logic that depends on a cached

response from that parameter.

Expiration and start/end dates

Course expiration is handled by ld_course_access_expired() and

ld_course_access_expires_on(). When an access record is expired and processing

is allowed by learndash_process_user_course_access_expire, LearnDash marks

learndash_course_expired_{$course_id}, revokes direct course access, fires

learndash_user_course_access_expired, and may delete progress depending on

course settings.

The Product model adds course start/end behavior:

paid/closed courses.

If a custom plugin grants access from an order/import and needs a historical

date, set the access-from date through ld_course_access_from_update() after

granting access.

Course structure is not access

Creating sfwd-lessons, sfwd-topic, or sfwd-quiz posts does not attach

them to a course structure. Course step order lives in LearnDash's course steps

model and related metadata. Use the course step APIs or REST /steps endpoint

when a task must build a usable course outline.

For course outline reads, use:

Common mistakes to reject

REST, group changes, imports, or background jobs.

Cross-references

hierarchical groups, and group leaders.

enrollment and LearnDash WooCommerce counters.

steps, or expose course data to headless clients.

progress activity, and incomplete-step navigation.

References

Validated against LearnDash LMS 5.1.9 local source:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.