Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LMNTL/8ab8209d2c303af9828a7547ae3d98cf to your computer and use it in GitHub Desktop.
Save LMNTL/8ab8209d2c303af9828a7547ae3d98cf to your computer and use it in GitHub Desktop.
Restrict membership access based on parent category
/*
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