Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Locking Down bbPress Forums using Paid Memberships Pro
/*
*****
UPDATE: This gist is no longer needed.
The better way to integrate PMPro and bbPress is to the use the pmpro-bbpress plugin,
which is now available in the WordPress repository:
http://wordpress.org/plugins/pmpro-bbpress/
*****
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(!empty($forum_id) && $post->ID == $forum_id)
return true;
elseif(empty($forum_id))
return true;
else
return false;
}
elseif(bbp_is_topic($post->ID))
{
if(!empty($forum_id) && $post->post_parent == $forum_id)
return true;
elseif(empty($forum_id))
return true;
else
return false;
}
elseif(bbp_is_reply($post->ID))
{
if(!empty($forum_id) && in_array($forum_id, $post->ancestors))
return true;
elseif(empty($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
You can’t perform that action at this time.