Last active
May 18, 2018 22:29
-
-
Save thomasplevy/373ce206533d0d777c13acc7d96c4df9 to your computer and use it in GitHub Desktop.
Custom restriction check to automatically restrict all itmes in a given post type to users nerolled in one or more membership levels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // don't include this line in your functions.php file | |
/** | |
* Custom restriction check to automatically restrict all itmes in a given post type | |
* to users nerolled in one or more membership levels | |
* @param array $results array or result info | |
* @return array | |
*/ | |
function my_llms_page_restrictions( $results ) { | |
// array of custom post type names | |
$my_post_types = array( | |
'my_custom_post_type', | |
); | |
// Array of WP_Post ID(s) of the membership(s) you wish to restrict this post type to | |
$membership_ids = array( | |
123, | |
456, | |
); | |
// url to redirect to when user does nt have access | |
$redirect_url = 'http://mycustomredirect.url'; | |
// only check if we're looking at the post type defined above | |
if ( in_array( get_post_type( $results['post_id'] ), $my_post_types ) ) { | |
$user_id = get_current_user_id(); | |
// loop through ids | |
foreach ( $membership_ids as $id ) { | |
// check access | |
$access = ( $user_id ) ? llms_is_user_enrolled( $user_id, $id ) : false; | |
// redirect if use is not logged in or does not have access | |
if ( ! $access ) { | |
wp_safe_redirect( $redirect_url ); | |
exit; | |
} | |
} | |
} | |
return $results; | |
} | |
add_filter( 'llms_page_restricted', 'my_llms_page_restrictions' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment