Reply To: [Resolved] Programatically add new members to a group on account activation
Ok, I solved the problem, turns out I was using wrong the $bp->loggedin_user->id because only returns a value when the user is loggedin, and I wanted to add members to groups when they activate their account, so, the return in that case (members activating their account are not logged in) is 0.
The same applys to the function: get_member_nfl_team(), it tries to get the ID of the logged in user.
I made some modifications to the code and worked perfectly:
function add_members_to_nfl_group($user_id){ // I suppose when you add the parameter $user_id in the fuction and then pass it trough the action 'bp_core_activated_user', the action returns the ID of the memeber beeing activated.
$nfl_team = bp_get_profile_field_data('field=2&user_id='.$user_id); // I erased the function get_member_nfl_team() and put the variable here with the new $user_id
$nfl_team_slug = str_replace(" ", "-", strtolower($nfl_team));
$group_slug = "aficionados-a-los-".$nfl_team_slug;
$group_id = BP_Groups_Group::group_exists($group_slug);
groups_join_group( $group_id, $user_id ); // Instead of groups_accept_invite() as @henrywright suggested
}
add_action( 'bp_core_activated_user', 'add_members_to_nfl_group' );
And thats all.