How to Make a Global Friend on BuddyPress

I was recently working on a BuddyPress site where the site owner wanted everyone who signed up to be a friend of a "support" user. He wanted people to be able to direct message this account, and in this case, he had Comet Chat installed. This meant that if "support" were online, then anyone could chat with support. I dug into the BuddyPress DB to see how friends were enabled and wrote this function that you can put into your themes functions.php to enable. The first step is to create the "support" user from the WordPress Admin Dashboard. Once created make a note of the User ID. A quick way to check the user id is to just edit the profile and look at the url. The user id located in the section "". The function utilizes the action "user_register" to handle associating the currently registered user with the "support" user.
initiator_user_id is the ID of the currently registered user. 
friend_user_id is the id of the global friend: in this case, "support" is ID 2.
is_confirmed just makes sure that neither the new user nor the "support" user needs to accept the friend invitation

function create_global_buddypress_friend($user_ID) {    
global $wpdb;
$now = date('Y-m-d H:i:s');
$wpdb->insert($wpdb->prefix . "bp_friends", array('initiator_user_id' => $user_ID,'friend_user_id' => '2','is_confirmed' => '1','date_created' => $now));
add_action('user_register', 'create_global_buddypress_friend');
Post a comment

Real Time Web Analytics ^