0
function _bp_enforce_bp_moderate_cap_for_admins( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
    // Bail if not checking the 'bp_moderate' cap.
    if ( 'bp_moderate' !== $cap ) {
        return $caps;
    }

    // Bail if BuddyPress is not network activated.
    if ( bp_is_network_activated() ) {
        return $caps;
    }

    // Never trust inactive users.
    if ( bp_is_user_inactive( $user_id ) ) {
        return $caps;
    }

    // Only users that can 'manage_options' on this site can 'bp_moderate'.
    return array( 'manage_options' );
}
add_filter( 'map_meta_cap', '_bp_enforce_bp_moderate_cap_for_admins', 10, 4 );

In the above code, the capability "bp_moderate" is mapped to "manage_options". This means any action that requires "bp_moderate" would be equivalent to requiring "manage_options". I have already given the user "bp_moderate" but that won't allow him to actually moderate because he doesn't have "manage_options". Now "manage_options" is too powerful, with it enabled, the user can do many things on the backend. So I certainly can't give "manage_options" to him. It would work for me if I could simply comment out the above code, but since that code resides in the plugin's core file, so I can't do that. The only option left for me is to write my own function to nullify what the above function has done when the current user is the user I want to grant the privilege to moderate:

function allow_daniel_edit( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
    if ($user_id == 59) {
        return $caps;
    }
    // Bail if not checking the 'bp_moderate' cap.
    if ( 'bp_moderate' !== $cap ) {
        return $caps;
    }

    // Bail if BuddyPress is not network activated.
    if ( bp_is_network_activated() ) {
        return $caps;
    }

    // Never trust inactive users.
    if ( bp_is_user_inactive( $user_id ) ) {
        return $caps;
    }

    // Only users that can 'manage_options' on this site can 'bp_moderate'.
    return array( 'manage_options' );
}
add_filter( 'map_meta_cap', 'allow_daniel_edit', 999, 4 );

But this didn't work, because by the time my function executes, the $caps has been altered by the previous code and becomes "manage_options". So what should be the right way to do this?

1 Answer 1

0

Try:

function allow_daniel_edit( $caps = array(), $cap = '', $user_id = 0, $args = array() ) {
    if ($user_id == 59) {
        return array( 'bp_moderate' );  
        // or some other permission or additional permissions
    }

    return $caps;
}
add_filter( 'map_meta_cap', 'allow_daniel_edit', 999, 4 );
1
  • You will need to check the value of $cap, otherwise you're setting everything to require bp_moderate. Commented Jan 8, 2023 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.