Skip to content

Instantly share code, notes, and snippets.

@dwanjuki
Last active June 26, 2024 14:22
Show Gist options
  • Save dwanjuki/2d084e8a81ace9f3741bbd3c895bfc93 to your computer and use it in GitHub Desktop.
Save dwanjuki/2d084e8a81ace9f3741bbd3c895bfc93 to your computer and use it in GitHub Desktop.
Restrict access to the BuddyPress directory based on membership level (Redirect away)
<?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