Home » Wordpress » Adding “Add Friend Button” anywhere in the buddypress site

Adding “Add Friend Button” anywhere in the buddypress site

Posted by: admin November 30, 2017 Leave a comment


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.

 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.



<?php if ( function_exists( 'bp_add_friend_button' ) ) : ?>
<?php bp_add_friend_button() ?>
<?php endif; ?>


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> ';