llms_is_post_restricted_by_membership( int $post_id, int|null $user_id = null )
Determine if a WordPress post (of any type) is restricted to at least one LifterLMS Membership level.
Description Description
This function replaces the now deprecated page_restricted_by_membership() (and has slightly different functionality).
Parameters Parameters
- $post_id
-
(int) (Required) WP_Post ID.
- $user_id
-
(int|null) (Optional) WP User ID (will use get_current_user_id() if none supplied). Default
null
.Default value: null
Return Return
(bool|int) WP_Post ID of the membership if a restriction is found. False if no restrictions found.
Source Source
File: includes/functions/llms.functions.access.php
function llms_is_post_restricted_by_membership( $post_id, $user_id = null ) { // don't check these posts types. $skip = apply_filters( 'llms_is_post_restricted_by_membership_skip_post_types', array( 'course', 'lesson', 'llms_quiz', 'llms_membership', 'llms_question', 'llms_certificate', 'llms_my_certificate', ) ); if ( in_array( get_post_type( $post_id ), $skip, true ) ) { return false; } $memberships = get_post_meta( $post_id, '_llms_restricted_levels', true ); $restricted = get_post_meta( $post_id, '_llms_is_restricted', true ); if ( 'yes' === $restricted && $memberships && is_array( $memberships ) ) { // if no user, return the first membership from the array as the restriction id. if ( ! $user_id ) { $restriction_id = array_shift( $memberships ); } else { $student = llms_get_student( $user_id ); if ( ! $student ) { $restriction_id = array_shift( $memberships ); } else { // reverse so to ensure that if user is in none of the memberships, // they'd encounter the same restriction settings as a visitor. $memberships = array_reverse( $memberships ); // loop through the memberships. foreach ( $memberships as $mid ) { // set this as the restriction id. $restriction_id = $mid; // once we find the student has access break the loop, // this will be the restriction that the template loader will check against later. if ( $student->is_enrolled( $mid ) ) { break; } } } } return absint( $restriction_id ); } return false; }
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
Version | Description |
---|---|
3.37.10 | Call in_array() with strict comparison. |
3.16.14 | Unknown. |
3.0.0 | Introduced. |