-
-
Save strangerstudios/3111478 to your computer and use it in GitHub Desktop.
| <?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); |
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. :-)
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.
Thank you for this. This was the only working snippet I could find to restrict members from accessing buddypress pages.
Also trying to implement this and quite new. Could someone tell me where to add this code?