Access to BudyPress menu for non-admin user

While BuddyPress introduces own ‘bp_moderate’ user capability, which will allow for a usual user access to all BuddyPress menus and functions, this capability is really mapped to the ‘manage_options’ user capability via ‘map_meta_cap’ filter.

Unfortunately it’s impossible just to re-map ‘bp_moderate’ to some other capability’ because of BuddyPress still uses ‘manage_options’ directly in its code.
For example at file bp-xprofile/bp-xprofile-admin.php, line #24 we see:

if ( ! bp_current_user_can( 'bp_moderate' ) ) {
	return false;
}

But at the next line it uses ‘manage_options’ directly instead of use ‘bp_moderate’:

add_users_page( _x( 'Profile Fields', 'xProfile admin page title', 'buddypress' ), _x( 'Profile Fields', 'Admin Users menu', 'buddypress' ), 'manage_options', 'bp-profile-setup', 'xprofile_admin' );

So, it’s more complex to grant to some non-admin user access to the “Users->Profile Fields” menu without granting him ‘manage_options’ capability.

One of existing workarounds is to grant to a user ‘manage_options’ capability, but block from him any extra functionality, like ‘Settings’ menu and menus added by other plugins. This is possible with “Admin menu access” add-on included into the User Role Editor Pro.

2nd way, is try to replace (re-map with additional ‘map_meta_cap’ filter) ‘manage_options’ and ‘bp_moderate’ capabilities with some other, like ‘bp_manage_xprofile_fields’ exceptionally for the selected (I selected ‘author’ for this example) role.

Code below allows this:

add_filter('map_meta_cap', 'remap_manage_options_for_profile_fields', 11, 4);
function remap_manage_options_for_profile_fields( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
    global $pagenow;
    
    if ($cap!='manage_options' && $cap!='bp_moderate') {
        return $caps;
    }
    
    // exclude recursion
    remove_filter('map_meta_cap', 'remap_manage_options_for_profile_fields', 11);
    // do not change anything for admin
    if (current_user_can('administrator')) {
        return $caps;
    }
    // do not change anything for roles other than 'author'
    if (!current_user_can('author')) {
        return $caps;
    }
    add_filter('map_meta_cap', 'remap_manage_options_for_profile_fields', 11, 4);
    
    $caps = array('bp_manage_xprofile_fields');
    
    return $caps;
}

You can setup it as a must-use plugin (separate .php file at wp-content/mu-plugins folder) or add it to functions.php file of your active theme.

Disadvantage – this code allows access to other BuddyPress menus like ‘Groups’, etc., as ‘bp_moderate’ allows that.

Advantage – we can do it without giving to a user very powerful ‘manage_options’ capability. Other BuddyPress menu options could be blocked with the same “Admin menu access” add-on included into the User Role Editor Pro.

In order user with ‘author’ role can access “Profile Fields” menu under “Users” menu using the code above you have to grant to the ‘author’ role: ‘list_users’, ‘edit_users’ and ‘bp_manage_xprofile_fields’ capabilities.

P.S. This post was written as an answer for the related question at User Role Editor support forum at wordpress.org

Share