Forked from ideadude/my_pmpro_bp_lockdown_all_bp.php
Created
April 4, 2020 14:08
Allow non-members to view the BuddyPress profile pages even if you are "locking down all of BuddyPress".
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
/** | |
* 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