Last active
June 26, 2024 14:22
-
-
Save dwanjuki/2d084e8a81ace9f3741bbd3c895bfc93 to your computer and use it in GitHub Desktop.
Restrict access to the BuddyPress directory based on membership level (Redirect away)
This file contains 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 | |
/** | |
* Restrict access to the BuddyPress directory based on membership level | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmpro_bp_directory_redirect() { | |
if( ! function_exists( 'bp_is_directory' ) || ! function_exists( 'pmpro_hasMembershipLevel' ) ) { | |
return; | |
} | |
// Redirect users without the specified membership levels away from the BuddyPress member directory page | |
if( bp_is_directory() && ! pmpro_hasMembershipLevel( array( '2','3' ) ) ) { | |
$redirect_url = site_url( 'path-to-page/' ); | |
wp_redirect( $redirect_url ); | |
exit(); | |
} | |
} | |
add_action( 'template_redirect', 'my_pmpro_bp_directory_redirect' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment