Skip to main content
added 264 characters in body
Source Link
shenkwen
  • 131
  • 1
  • 9
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 want to grant a certain user "bp_moderate" but withoutcertainly can't give "manage_options" abilityto him. SoIt would work for me if I am thinkingcould 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 a callbackmy own function to overridenullify what the function 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?

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". Now "manage_options" is too powerful, with it enabled, the user can do many things on the backend. I want to grant a certain user "bp_moderate" but without "manage_options" ability. So I am thinking to write a callback function to override the function above:

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?

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?

Source Link
shenkwen
  • 131
  • 1
  • 9

How to correctly override a filter?

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". Now "manage_options" is too powerful, with it enabled, the user can do many things on the backend. I want to grant a certain user "bp_moderate" but without "manage_options" ability. So I am thinking to write a callback function to override the function above:

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?