Adding “Add Friend Button” anywhere in the buddypress site

I’m trying to place an “Add Friend” button outside of the members page, inside a loop, so the button appears next to the author’s avatar in a post.
I tried adding ;

<?php if ( bp_is_active( 'friends' ) ) { ?>

                        <?php bp_add_friend_button( $user_ids['users'][$i]->id ) ?>

                    <?php } ?>

Inside the loop but it didn’t return any results.
Then I placed the following action hook in functions.php, it displayed the button inside the loop but once clicked, all the buttons in the post list got clicked as well.

Read More
 add_action( ‘the_content’, ‘bp_add_friend_button’, 5 );

So now I’m stuck. I thought, adding a template tag inside the template would work since it had worked for “Send Message” button.

Related posts

Leave a Reply

3 comments

  1. Here is an example that works for me, hope its helps.

    $id_or_email = $result -> user_ID;
    $avatar_default = get_avatar($id_or_email, $size, $default = '', $alt = '', $args = null);
    $potential_friend_id = $id_or_email;
    $friend_status = false;
    $friend_button = bp_add_friend_button($potential_friend_id, $friend_status);
     echo '<li>'. $avatar_default. $user_info->user_nicename . $friend_button . '</li> ';
    
  2. There may be a plugin that solve this, but to followup on Rafa’s solution.
    This complete code worked for me.

    1. In bp-custom.php ( this is a file you need to create)

       function my_id_text_processor($friend_id)
       {
         //Return string id without 'uid-' attached
         $search = 'uid-';
         $replace_with = '';
         return  str_replace($search, $replace_with, $friend_id);
      
       }
      
      1. In the file you want the button to appear put this code.
       < ?php $friend_id = my_id_text_processor(bp_get_group_invite_item_id()) ? >
      
      
       $avatar_default = get_avatar($friend_id, $size, $default = '', $alt = '', $args = null);
       $friend_status = false;
       $friend_button = bp_add_friend_button($friend_id, $friend_status);
       echo '<li>'. $avatar_default,bp_group_invite_user_link(),$friend_button . '</li> ';