Skip to content

Instantly share code, notes, and snippets.

@ideadude
Last active October 27, 2020 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideadude/dde67a868aa527f8e2793c1cf80bf6b9 to your computer and use it in GitHub Desktop.
Save ideadude/dde67a868aa527f8e2793c1cf80bf6b9 to your computer and use it in GitHub Desktop.
Hide BuddyPress docs from members of some levels when using Paid Memberships Pro.
/**
* Hide docs from some levels.
* Make sure to adjust the pmpro_hasMembershipLevel check below
* to use the IDs of your levels which SHOULD have access to view members.
* Everyone else will be redirected to the restricted access or levels page.
*
* Copy this code into a custom plugin or Code Snippet.
*/
function my_redirect_away_from_docs() {
// Bail if PMPro and BuddyPress aren't active.
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) {
return;
}
// Bail if we're not on a doc page
if ( ! is_singular( 'bp_doc' ) ) {
return;
}
// Bail if the user has a good membership level.
// IMPORTANT: Update the levels checked here.
if ( pmpro_hasMembershipLevel( array( 1, 4, 5 ) ) ) {
return;
}
// Redirect everyone else to the restricted page.
if ( function_exists( 'pmpro_bp_redirect_to_access_required_page' ) ) {
pmpro_bp_redirect_to_access_required_page();
exit;
} else {
global $pmpro_pages;
wp_redirect( get_permalink( $pmpro_pages['levels'] ) );
exit;
}
}
add_action( 'template_redirect', 'my_redirect_away_from_docs' );
// Block these download document file URLs as well.
function my_block_download_document_file_urls() {
// Bail if PMPro and BuddyPress aren't active.
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) {
return;
}
// Bail if this is not a downlooad
if ( empty( $_REQUEST['download_document_file'] ) ) {
return;
}
// Bail if the user has a good membership level.
// IMPORTANT: Update the levels checked here.
if ( pmpro_hasMembershipLevel( array( 1, 4, 5 ) ) ) {
return;
}
// Redirect everyone else to the restricted page.
if ( function_exists( 'pmpro_bp_redirect_to_access_required_page' ) ) {
pmpro_bp_redirect_to_access_required_page();
exit;
} else {
global $pmpro_pages;
wp_redirect( get_permalink( $pmpro_pages['levels'] ) );
exit;
}
}
add_action( 'init', 'my_block_download_document_file_urls', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment