Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Hook into the user registration process
- add_action('user_register', 'vap_send_friend_request_to_new_user', 10, 1);
- function vap_send_friend_request_to_new_user($user_id) {
- // Check if BuddyPress is active
- if ( function_exists( 'bp_is_active' ) && bp_is_active('friends') ) {
- // Get the IDs of the users (sender and receiver)
- $sender_id = 1; // Replace with the actual sender's user ID
- $receiver_id = $user_id; // The newly registered user
- // Check if friendship doesn't already exist
- if ( ! friends_check_friendship( $sender_id, $receiver_id ) ) {
- // Send friend request
- $friendship_id = friends_add_friend( $sender_id, $receiver_id, true );
- if ( ! is_wp_error( $friendship_id ) ) {
- // Friend request sent successfully
- // You can log or output a success message here
- error_log('Friend request sent to user ID ' . $receiver_id);
- } else {
- // Error sending friend request
- // You can log or output an error message here
- error_log('Error sending friend request to user ID ' . $receiver_id . ': ' . $friendship_id->get_error_message());
- }
- } else {
- // Friendship already exists
- // You can log or output a message here
- error_log('Friendship already exists between users ' . $sender_id . ' and ' . $receiver_id);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement