Advertisement
vapvarun

Send friend request to all newly registered users

Nov 15th, 2023
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | Software | 0 0
  1. // Hook into the user registration process
  2. add_action('user_register', 'vap_send_friend_request_to_new_user', 10, 1);
  3.  
  4. function vap_send_friend_request_to_new_user($user_id) {
  5.     // Check if BuddyPress is active
  6.     if ( function_exists( 'bp_is_active' ) && bp_is_active('friends') ) {
  7.         // Get the IDs of the users (sender and receiver)
  8.         $sender_id = 1; // Replace with the actual sender's user ID
  9.         $receiver_id = $user_id; // The newly registered user
  10.  
  11.         // Check if friendship doesn't already exist
  12.         if ( ! friends_check_friendship( $sender_id, $receiver_id ) ) {
  13.             // Send friend request
  14.             $friendship_id = friends_add_friend( $sender_id, $receiver_id, true );
  15.  
  16.             if ( ! is_wp_error( $friendship_id ) ) {
  17.                 // Friend request sent successfully
  18.                 // You can log or output a success message here
  19.                 error_log('Friend request sent to user ID ' . $receiver_id);
  20.             } else {
  21.                 // Error sending friend request
  22.                 // You can log or output an error message here
  23.                 error_log('Error sending friend request to user ID ' . $receiver_id . ': ' . $friendship_id->get_error_message());
  24.             }
  25.         } else {
  26.             // Friendship already exists
  27.             // You can log or output a message here
  28.             error_log('Friendship already exists between users ' . $sender_id . ' and ' . $receiver_id);
  29.         }
  30.     }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement