Last active
April 23, 2024 12:52
-
-
Save strangerstudios/3111478 to your computer and use it in GitHub Desktop.
Lockdown BuddyPress with Paid Memberships Pro Example
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
<?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 : [email protected]) | |
*/ | |
//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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this. This was the only working snippet I could find to restrict members from accessing buddypress pages.