Skip to content

Instantly share code, notes, and snippets.

@ideadude
Created April 1, 2020 16:28
Show Gist options
  • Save ideadude/ad1fcb93fc465a855e65ac7e9c04314c to your computer and use it in GitHub Desktop.
Save ideadude/ad1fcb93fc465a855e65ac7e9c04314c to your computer and use it in GitHub Desktop.
Allow non-members to view the BuddyPress profile pages even if you are "locking down all of BuddyPress".
/**
* This gist will disable the default behavior in PMPro BuddyPress 5.2.5 or earlier,
* which would redirect users away from the BP profile pages if you had
* "all of BuddyPress" locked down for non-members or members of a specific level.
*
* We might address this in future versions of the Add-On, but for now, you can use
* this gist. Add it to a Code Snippet or a custom plugin.
*/
// First disable the default PMPro BuddyPress behavior.
function my_disable_pmprobbp_lockdown_all_bp() {
remove_action( 'template_redirect', 'pmpro_bp_lockdown_all_bp', 50 );
}
add_action( 'plugins_loaded', 'my_disable_pmprobbp_lockdown_all_bp' );
// Our new lockdown code, which allows non-members to see profiles.
function my_pmpro_bp_lockdown_all_bp() {
if ( !function_exists( 'pmpro_getMembershipLevelForUser' ) ) {
return;
}
if( !function_exists( 'is_buddypress') || !is_buddypress() ) {
return;
}
// Don't redirect away from the Register or Activate pages if using BuddyPress registration.
$register_page = get_option( 'pmpro_bp_registration_page' );
if ( 'buddypress' == $register_page && in_array( bp_current_component(), array( 'register', 'activate' ) ) ) {
return;
}
// START: THIS IS THE PART ADDED IN THIS SNIPPET
if ( bp_is_my_profile() ) {
return;
}
// END: THIS IS THE PART ADDED IN THIS SNIPPET
global $current_user;
$user_id = $current_user->ID;
if( !empty( $user_id ) ) {
$level = pmpro_getMembershipLevelForUser( $user_id );
}
if( !empty( $level ) ) {
$level_id = $level->id;
} else {
$level_id = 0; //non-member user
}
$pmpro_bp_options = pmpro_bp_get_level_options( $level_id );
if( $pmpro_bp_options['pmpro_bp_restrictions'] == -1 ) {
pmpro_bp_redirect_to_access_required_page();
}
}
add_action( 'template_redirect', 'my_pmpro_bp_lockdown_all_bp', 50 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment