The course access helper
pom_lms_user_can_access_course( int $course_id ): bool
Returns whether the current user may access a course.
What it evaluates
In this order, stopping at the first that applies:
| Step | Condition | Result |
|---|---|---|
| 1 | The course requires login and the visitor is signed out | false |
| 2 | The course has a permission rule | Whatever the rule says |
| 3 | The course is marked as paid | Whether the user has a qualifying purchase |
| 4 | None of the above | true |
Steps 2 and 3 do not combine. A course with a rule never reaches the purchase check, whatever its paid flag says — a fact worth remembering when a course behaves unexpectedly.
Using it
if ( ! function_exists( 'pom_lms_user_can_access_course' ) ) {
return '';
}
if ( ! pom_lms_user_can_access_course( $course_id ) ) {
return '';
}
// Safe to render course content.
Signed-out visitors
The helper handles them. A course with no login requirement, no rule and no paid flag returns true for anonymous visitors, which is exactly what a public course should do.
Do not add your own is_user_logged_in() check in front of it — that changes the behavior of open courses.
Current user only
There is no user-ID parameter. The helper answers for the request it is called in.
That rules it out for scheduled tasks, bulk reports and email generation. If you find yourself wanting to loop over users and ask this question, stop: there is no supported way to do it, and reconstructing the answer from the underlying data reimplements the precedence the helper exists to own.
Use the lesson helper for lessons
Course access is not lesson access. Lessons can carry their own permission rule and can be marked as previews, so a lesson may be open when the course is closed, or closed when the course is open.
For lesson content, always call the lesson access helper.
Cost
The helper is cheap in the common cases — a couple of meta reads and, at most, a rule evaluation.
The expensive case is a paid course, where the purchase check inspects the customer's orders. If you are listing many courses in one request, expect that cost per paid course.
Call it once per course per request and keep the result in a local variable rather than calling it repeatedly in a template.
Typical uses
A shortcode that embeds course material:
if ( ! pom_lms_user_can_access_course( $course_id ) ) {
return '';
}
Filtering a course list to what a visitor can open:
$visible = array_filter( $course_ids, 'pom_lms_user_can_access_course' );
That is what the My courses table does, and it is why an inaccessible course is absent rather than greyed out.
Deciding whether to show a buy button: invert it. A visitor who cannot access a paid course is the one who should see the product link.
What it does not tell you
- Why access was granted or refused.
- Whether the user will have access later, or had it before.
- Anything about progress.