Skip to content

Instantly share code, notes, and snippets.

@christopherlai
Forked from strangerstudios/gist:1633637
Created September 15, 2013 02:43
Show Gist options
  • Save christopherlai/6567606 to your computer and use it in GitHub Desktop.
Save christopherlai/6567606 to your computer and use it in GitHub Desktop.
/*
These next two functions work together to lock down bbPress forums based on PMPro membership level.
Check that the current user has access to this forum. Be sure to update the $restricted_forums array based on your needs.
*/
function pmpro_check_forum()
{
global $current_user;
/*
This array denotes which forums require which membership level.
The format is $restricted_forums[forum_id] = array(membership_id, membership_id, membership_id, etc)
*/
$restricted_forums[207] = array(1,2);
//is this even a forum page at all?
if(!bbp_is_forum_archive() && pmpro_bbp_is_forum())
{
//now check this post
foreach($restricted_forums as $forum_id => $membership_levels)
{
if(pmpro_bbp_is_forum($forum_id))
{
//we found the forum, now check the membership level
if(pmpro_hasMembershipLevel($membership_levels))
return true;
else
{
//need to redirect away
wp_redirect(home_url("/forums/"));
exit;
}
}
}
}
}
add_action("wp", "pmpro_check_forum");
/*
Function to tell if the current forum, topic, or reply is a subpost of the forum_id passed.
If no forum_id is passed, it will return true if it is any forum, topic, or reply.
*/
function pmpro_bbp_is_forum($forum_id = NULL)
{
global $post;
if(bbp_is_forum($post->ID))
{
if($forum_id && $post->ID == $forum_id)
return true;
elseif(!$forum_id)
return true;
else
return false;
}
elseif(bbp_is_topic($post->ID))
{
if($forum_id && $post->post_parent == $forum_id)
return true;
elseif(!$forum_id)
return true;
else
return false;
}
elseif(bbp_is_reply($post->ID))
{
if($forum_id && in_array($forum_id, $post->ancestors))
return true;
elseif(!$forum_id)
return true;
else
return false;
}
else
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment