How to auto-accept a friend-request in buddypress based on user meta

I’m still getting my head around buddypress, so far so good but I’ve hit a snag where I want certain users who are flagged as an ‘expert’ to automatically accept any friend requests people make.

I’ve found a couple of functions related to this but I think I’m missing something that would make this simpler such as setting a constant or overriding part of the $bp global…

Read More

What I have so far is the following:

function bp_auto_accept_friend_request( $friendship_id, $friendship_initiator_id, $friendship_friend_id ) {
    if ( is_user_expert( $friendship_friend_id ) ) {
        // force add
        friends_accept_friendship( $friendship_id );
        friends_add_friend( $friendship_initiator_id, $friendship_friend_id, true );
    }
}
add_action('friends_friendship_requested', 'bp_auto_accept_friend_request', 200, 3);

Can anyone tell me where I should be looking to make this nice and seamless just as if the main settings were set to bypass the request process please?

Related posts

Leave a Reply

1 comment

  1. UPDATE

    try this

    function bp_auto_accept_friend_request( $friendship_id, $friendship_initiator_id, $friendship_friend_id ) {
            $friendship_status = BP_Friends_Friendship::check_is_friend( $friendship_initiator_id, $friendship_friend_id );
            if ( 'not_friends' == $friendship_status ) {
                if ( is_user_expert( $friendship_friend_id ) ) {
                // force add
                friends_add_friend( $friendship_initiator_id, $friendship_friend_id, true );
                friends_accept_friendship( $friendship_id );
            }
            }
        }
        add_action('friends_friendship_requested', 'bp_auto_accept_friend_request', 200, 3);
    

    this way we only call friends_add_friend function with $force = true if they are not friends yet.