Last active
August 8, 2025 07:23
-
-
Save cryptexvinci/3b46f957aaa6551b1df1d9d038504954 to your computer and use it in GitHub Desktop.
Ultimate Member - Friends Extensions - Automatically add specific users as friends when a new user registers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Automatically add specific users as friends when a new user registers | |
| * | |
| * This function hooks into the Ultimate Member user registration process | |
| * and automatically creates friend connections between new users and | |
| * a predefined list of existing members. | |
| */ | |
| // Hook into the UM user registration process | |
| // Priority 5, accepts 2 parameters (user_id and args) | |
| add_action('um_user_register', 'um_auto_friend_members', 5, 2); | |
| /** | |
| * Auto-friend specific members when a user registers | |
| * | |
| * @param int $user_id The ID of the newly registered user | |
| * @param array $args Registration arguments from Ultimate Member | |
| */ | |
| function um_auto_friend_members($user_id, $args) { | |
| // Define the list of usernames to automatically friend | |
| $friend_users = array( | |
| 'rjtmusic', | |
| 'james-peacock', | |
| 'carl-thomson', | |
| 'jonathan-proud', | |
| 'stu-dawson', | |
| 'carly-mckee', | |
| ); | |
| // Loop through each username in the friend list | |
| foreach ($friend_users as $username) { | |
| // Get the user object by username | |
| $friend_user = get_user_by('login', $username); | |
| // Check if the user exists before attempting to add friendship | |
| if ($friend_user) { | |
| // Use Ultimate Member's Friends API to create the friendship | |
| // Parameters: friend_user_id, current_user_id | |
| UM()->Friends_API()->api()->add($friend_user->ID, $user_id); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment