Forked from greathmaster/pmpro-restrict-posts-based-on-parent-category.php
Last active
May 30, 2019 20:50
-
-
Save LMNTL/8ab8209d2c303af9828a7547ae3d98cf to your computer and use it in GitHub Desktop.
Restrict membership access based on parent category
This file contains hidden or 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
/* | |
Restrict membership access based on parent category | |
*/ | |
function category_restrictions($hasaccess, $mypost, $myuser, $post_membership_levels) | |
{ | |
global $post, $current_user; | |
$categories = wp_get_post_categories($mypost->ID); | |
$restrict = false; | |
$level = pmpro_getMembershipLevelForUser($myuser->ID); | |
if(!empty($level)) | |
$level_id = $level->id; | |
else | |
$level_id = 0; | |
foreach($categories as $category) | |
{ | |
$parent_category = get_category($category)->parent; | |
$restricted_cats = pmpro_getMembershipCategories($level_id); | |
if(!empty($parent_category) && !in_array($parent_category, $restricted_cats)) | |
return false; | |
} | |
return $hasaccess; | |
} | |
add_filter('pmpro_has_membership_access_filter', 'category_restrictions', 10, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment