Skip to content

Instantly share code, notes, and snippets.

@dmasotti
Last active February 26, 2020 15:49
Show Gist options
  • Save dmasotti/b954ac1a9096c3d544ff6ad857cb0d30 to your computer and use it in GitHub Desktop.
Save dmasotti/b954ac1a9096c3d544ff6ad857cb0d30 to your computer and use it in GitHub Desktop.
LearnDash Filter Enroll Courses Based on existing RCP membership
/**
* LearnDash Filter Enroll Courses Based on existing RCP membership
*
* This filter allows users to access specific course(s) based on user role or capabilities.
*
* @param int $post_id This is the ID of the Course to check.
* @param int $user_id This is the ID of the logged in user to check.
* @param $allowed_course_ids array This is an array of the accessible courses if $return is true
*
* @return Should be true to grant access to the specified course(s)
*/
add_filter( 'sfwd_lms_has_access', function( $return, $post_id, $user_id ) {
if ( empty( $user_id ) ) {
if ( !is_user_logged_in() ) {
return $return;
} else {
$user_id = get_current_user_id();
}
}
$course_id = learndash_get_course_id( $post_id );
if( empty($course_id)) {
return $return;
} else {
$allowed_course_ids = array( );
if( !in_array($course_id, $allowed_course_ids))
return $return;
// User must have specified role in order to access the courses listed in the $allowed_course_ids array
/* if(user_can($user_id, "subscriber"))
$return = true;
Example 2: User must have specified capability in order to access the courses listed in the $allowed_course_ids array
if(user_can($user_id, "3a_textbook"))
$return = true; */
$return = rcp_user_has_active_membership($user_id);
}
return $return;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment