Reply To: How to make a custom add friend link in buddypress
Hi @jaybee08
Building the link isn’t straightforward because there are a number of cases to consider. To take all of the conditions into account it is necessary to use a switch statement like this:
$is_friend = bp_is_friend( $potential_friend_id );
if ( empty( $is_friend ) )
return false;
switch ( $is_friend ) {
case 'pending' :
$link = wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/cancel/' . $potential_friend_id . '/', 'friends_withdraw_friendship' );
break;
case 'awaiting_response' :
$link = bp_loggedin_user_domain() . bp_get_friends_slug() . '/requests/';
break;
case 'is_friend' :
$link = wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/remove-friend/' . $potential_friend_id . '/', 'friends_remove_friend' );
break;
default:
$link = wp_nonce_url( bp_loggedin_user_domain() . bp_get_friends_slug() . '/add-friend/' . $potential_friend_id . '/', 'friends_add_friend' );
break;
}
echo $link;
Note: You will need to provide the value of $potential_friend_id yourself. For example, if you know the ID of the potential friend is 8 then do this at the very top of that code:
$potential_friend_id = 8;
Alternatively, bp_displayed_user_id() will get the ID of the currently displayed member.