Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active April 23, 2024 12:52
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save strangerstudios/3111478 to your computer and use it in GitHub Desktop.
Save strangerstudios/3111478 to your computer and use it in GitHub Desktop.
Lockdown BuddyPress with Paid Memberships Pro Example
<?php
/*
Plugin Name: PMPro BuddyPress Customizations
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-buddypress-customizations/
Description: Example code to lock down parts of BuddyPress with PMPro
Version: 0.2
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
*/
/*
Copyright 2010 Stranger Studios (email : jason@strangerstudios.com)
*/
//disable buddy press admin bar
define('BP_DISABLE_ADMIN_BAR', true);
//hide some pages in buddypress
function pmpros_hide_buddy_press_pages()
{
$uri = $_SERVER['REQUEST_URI'];
$not_allowed = array(
//"/members/",
"/groups/",
"/groups/create/",
"/groups/create/*",
"/members/groups/",
"/members/*/settings/",
);
foreach($not_allowed as $check)
{
//prep for regex
$check = str_replace("/", "[\/]+", $check); //replace /
$check = str_replace("*\/", "[^\/]*\/", $check); //replace * in middle of URIs
$check = str_replace("+*", "+.*", $check); //replace * at end of URIs
$check = "/^" . $check . "\/?$/"; //add start/end /
if(preg_match($check, $uri))
{
wp_redirect(home_url());
exit;
}
}
//redirect groups to the forum
$redirects = array(
"/groups/*",
"/groups/*/home",
"/groups/*/members",
);
foreach($redirects as $check)
{
//prep for regex
$check = str_replace("/", "[\/]+", $check); //replace /
$check = str_replace("*", "([^\/]*)", $check); //replace *
$check = "/^" . $check . "\/?/"; //add start/end /
$matched = preg_match($check, $uri, $matches);
if(!empty($matches[1]))
{
//but check if we're already on the forums
$new_uri = "/groups/" . $matches[1] . "/forum/";
if(strpos($uri, $new_uri) === false)
{
wp_redirect(home_url("/groups/" . $matches[1] . "/forum/"));
exit;
}
}
}
//lock some things for members only
$members_only = array(
"/members/",
);
foreach($members_only as $check)
{
//prep for regex
$check = str_replace("/", "[\/]+", $check); //replace /
$check = str_replace("*\/", "[^\/]*\/", $check); //replace * in middle of URIs
$check = str_replace("+*", "+.*", $check); //replace * at end of URIs
$check = "/^" . $check . "\/?/"; //add start/end /
//make sure they are a member
if(preg_match($check, $uri) && !pmpro_hasMembershipLevel())
{
wp_redirect(pmpro_url("levels"));
exit;
}
}
}
add_action("init", "pmpros_hide_buddy_press_pages");
//add all members to the members buddy press group
function pmpros_add_members()
{
global $wpdb, $current_user;
if($current_user->user_login != "admin")
return false;
$users = $wpdb->get_results("SELECT * FROM $wpdb->pmpro_memberships_users WHERE membership_id = 6");
foreach($users as $user)
{
$already_member = $wpdb->get_var("SELECT id FROM wp_bp_groups_members WHERE user_id = '" . $user->user_id . "' LIMIT 1");
if(!$already_member)
{
$sqlQuery = "INSERT INTO wp_bp_groups_members (group_id, user_id, user_title, is_confirmed) VALUES('3', '" . $user->user_id . "', 'Member', '1')";
//echo $sqlQuery . "<hr />";
$wpdb->query($sqlQuery);
}
}
}
//add_action("init", "pmpros_add_members");
//add new members to the members buddy press group
function pmpros_pmpro_after_checkout($user_id)
{
global $wpdb;
$sqlQuery = "SELECT id FROM wp_bp_groups_members WHERE group_id = '3' AND user_id = '" . $user_id . "' LIMIT 1";
$already_member = $wpdb->get_var($sqlQuery);
if(!$already_member)
{
$sqlQuery = "INSERT INTO wp_bp_groups_members (group_id, user_id, user_title, is_confirmed) VALUES('3', '" . $user_id . "', 'Member', '1')";
$wpdb->query($sqlQuery);
}
}
//add_action("pmpro_after_checkout", "pmpros_pmpro_after_checkout");
//remove canceled members from the members buddy press group
function pmpros_pmpro_after_change_membership_level($level_id, $user_id)
{
global $wpdb;
if($level_id == 6 || $level_id == 15 || $level_id == 16)
{
$sqlQuery = "SELECT id FROM wp_bp_groups_members WHERE group_id = '3' AND user_id = '" . $user_id . "' LIMIT 1";
$already_member = $wpdb->get_var($sqlQuery);
if(!$already_member)
{
$sqlQuery = "INSERT INTO wp_bp_groups_members (group_id, user_id, user_title, is_confirmed) VALUES('3', '" . $user_id . "', 'Member', '1')";
$wpdb->query($sqlQuery);
}
}
else
{
$sqlQuery = "DELETE FROM wp_bp_groups_members WHERE group_id = 3 AND user_id = '" . $user_id . "' LIMIT 1";
$wpdb->query($sqlQuery);
}
}
add_action("pmpro_after_change_membership_level", "pmpros_pmpro_after_change_membership_level", 10, 2);
@andybuddha
Copy link

So where do I place this code later on?

@foxybid
Copy link

foxybid commented Mar 14, 2014

Hi, as I have this plugin upload? How normal can not be recorded.

@Tomaxim
Copy link

Tomaxim commented Nov 3, 2014

Just Purchased PMPro plugin and now to see that the guys above me post the BuddyPress issue/question
and still did not got answer .... small like $97 went down the drain ....

@Tomaxim
Copy link

Tomaxim commented Nov 3, 2014

Oh yah i forgot to ask are you going to help us or you preparing us christmas present and wrap it for us as plugin?

@strangerstudios
Copy link
Author

@Tomaxim, did you ever get a hold of us in our member forums? Let me know the name/email/etc you signed up under so I can make sure you get helped or get your money back.

@petervangorder
Copy link

I am in need of a similar solution, but I have multiple membership levels, each going to a corresponding group.

I see that in the function:
pmpros_pmpro_after_checkout
you INSERT INTO wp_bp_groups_members a group_id

But how will I know, in that function, what what level they signed up for?
The only param coming into the function is the $user_id

Advice or suggestions please. : )

@antanast
Copy link

Also trying to implement this and quite new. Could someone tell me where to add this code?

@markwoodroof
Copy link

markwoodroof commented Aug 21, 2016

You can add it to your functions.php or save the above code as pmp-buddyrpress.php and put it in a folder in your plugins folder. See https://www.elegantthemes.com/blog/tips-tricks/how-to-create-a-wordpress-plugin for some basic info on writing your own plugin...but this has all been done for you...you just need to install it. :-)

@xensor
Copy link

xensor commented Jun 24, 2017

BTW, if you wish to add more to the plugin that block pages to non-members you can edit this section:

//lock some things for members only
$members_only = array(
"/members/",
"/activity/",
"/forums/",
"/groups/",
"/register/"
);
foreach($members_only as $check)
{
//prep for regex
$check = str_replace("/", "[/]+", $check); //replace /
$check = str_replace("/", "[^\/]/", $check); //replace * in middle of URIs
$check = str_replace("+", "+.", $check); //replace * at end of URIs
$check = "/^" . $check . "/?/"; //add start/end /

	//make sure they are a member
	if(preg_match($check, $uri) && !pmpro_hasMembershipLevel())
	{			
		wp_redirect(pmpro_url("levels"));
		exit;
	}
}	

}
add_action("init", "pmpros_hide_buddy_press_pages");

All you need to do is name it a file and upload it into the plugin folder and activate it. This is already set up as a plugin for wordpress.

@mathysp
Copy link

mathysp commented Feb 16, 2018

Thank you for this. This was the only working snippet I could find to restrict members from accessing buddypress pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment