Skip to content

Instantly share code, notes, and snippets.

@imath
Created March 5, 2016 11:53
Show Gist options
  • Save imath/f589dcb5ac14c82051ae to your computer and use it in GitHub Desktop.
Save imath/f589dcb5ac14c82051ae to your computer and use it in GitHub Desktop.
Restrict BuddyDrive to a specific capability. More info about bp-custom.php > https://codex.buddypress.org/themes/bp-custom-php/
<?php
/**
* Get the required cap.
*
* For info about Capabilities
* @see https://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table
*/
function wpsouf_get_required_capability() {
return 'manage_options';
}
/**
* Restrict user nav to a specific cap.
*/
function wpsouf_restrict_nav_to_capability() {
$bd = buddydrive();
$user_id = bp_displayed_user_id();
if ( ! $user_id ) {
return;
}
// Check user's cap and eventually remove BuddyDrive Nav
if ( ! user_can( $user_id, wpsouf_get_required_capability() ) ) {
bp_core_remove_nav_item( 'buddydrive' );
// If current user
if ( (int) $user_id === (int) bp_loggedin_user_id() ) {
$bd->user_hasno_access = true;
}
}
}
add_action( 'bp_buddydrive_setup_nav', 'wpsouf_restrict_nav_to_capability' );
/**
* Restrict WP Admin Bar or BuddyDrive user link to a specific cap.
*/
function wpsouf_restrict_admin_bar_and_link_to_capability( $retval = '' ) {
$bd = buddydrive();
$user_id = bp_loggedin_user_id();
if ( ! $user_id ) {
return $retval;
}
if ( isset( $bd->user_hasno_access ) ) {
return false;
}
if ( ! user_can( $user_id, wpsouf_get_required_capability() ) ) {
return false;
}
return $retval;
}
add_filter( 'bp_buddydrive_admin_nav', 'wpsouf_restrict_admin_bar_and_link_to_capability' );
add_filter( 'buddydrive_user_buddydrive_url', 'wpsouf_restrict_admin_bar_and_link_to_capability' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment