friends_add_friend

Create a new friendship.

Description

friends_add_friend( (int) $initiator_userid, (int) $friend_userid, (bool) $force_accept = false ); 

Parameters (3)

0. $initiator_userid (int)
ID of the initiator user (the user who is sending the friendship request).
1. $friend_userid (int)
ID of the friend user (the user whose friendship is being requested).
2. $force_accept — Optional. (bool) => false
Whether to force acceptance. When false, running friends_add_friend() will result in a friendship request. When true, running friends_add_friend() will result in an accepted friendship, with no notifications being sent. Default: false.

Usage

  1. if ( !function_exists( 'friends_add_friend' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-friends/bp-friends-functions.php'; 
  3.  
  4. // ID of the "initiator" user (the user who is 
  5. // sending the friendship request). 
  6. $initiator_userid = -1; 
  7.  
  8. // ID of the "friend" user (the user whose friendship 
  9. // is being requested). 
  10. $friend_userid = -1; 
  11. $force_accept = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = friends_add_friend($initiator_userid, $friend_userid, $force_accept); 
  15.  

Defined (1)

The function is defined in the following location(s).

/bp-friends/bp-friends-functions.php  
  1. function friends_add_friend( $initiator_userid, $friend_userid, $force_accept = false ) { 
  2.  
  3. // You cannot be friends with yourself! 
  4. if ( $initiator_userid == $friend_userid ) { 
  5. return false; 
  6.  
  7. // Check if already friends, and bail if so. 
  8. if ( friends_check_friendship( $initiator_userid, $friend_userid ) ) { 
  9. return true; 
  10.  
  11. // Setup the friendship data. 
  12. $friendship = new BP_Friends_Friendship; 
  13. $friendship->initiator_user_id = $initiator_userid; 
  14. $friendship->friend_user_id = $friend_userid; 
  15. $friendship->is_confirmed = 0; 
  16. $friendship->is_limited = 0; 
  17. $friendship->date_created = bp_core_current_time(); 
  18.  
  19. if ( ! empty( $force_accept ) ) { 
  20. $friendship->is_confirmed = 1; 
  21.  
  22. // Bail if friendship could not be saved (how sad!). 
  23. if ( ! $friendship->save() ) { 
  24. return false; 
  25.  
  26. // Send notifications. 
  27. if ( empty( $force_accept ) ) { 
  28. $action = 'requested'; 
  29.  
  30. // Update friend totals. 
  31. } else { 
  32. $action = 'accepted'; 
  33. friends_update_friend_totals( $friendship->initiator_user_id, $friendship->friend_user_id, 'add' ); 
  34.  
  35. /** 
  36. * Fires at the end of initiating a new friendship connection. 
  37. * This is a variable hook, depending on context. 
  38. * The two potential hooks are: friends_friendship_requested, friends_friendship_accepted. 
  39. * @since 1.0.0 
  40. * @param int $id ID of the pending friendship connection. 
  41. * @param int $initiator_user_id ID of the friendship initiator. 
  42. * @param int $friend_user_id ID of the friend user. 
  43. * @param object $friendship BuddyPress Friendship Object. 
  44. */ 
  45. do_action( 'friends_friendship_' . $action, $friendship->id, $friendship->initiator_user_id, $friendship->friend_user_id, $friendship ); 
  46.  
  47. return true;