Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Programatically add new members to a group on account activation

  • @laloptk

    Participant

    WP version: 4.2 on localhost XAMPP
    BP version: 2.2.2.1
    WP Theme: TwentyFifteen

    I created bp-custom.php file and I placed the following code there:

    //get user's nfl team from custom field in registration form
    
    function get_member_nfl_team() {    
        $user_nfl_team = bp_get_profile_field_data('field=2&user_id='.bp_loggedin_user_id());
        return $user_nfl_team;
    }
    
    //Autommatically add new members to their NFL team's group
    
    function add_members_to_nfl_group(){    
        global $bp;
        $bp_user_id = $bp->loggedin_user->id; //The user id, it works.
        $nfl_team = get_member_nfl_team(); // The team the user chose, it works.
        $nfl_team_slug = str_replace(" ", "-", strtolower($nfl_team)); //Part of the slug of the group, it works
        $group_slug = "aficionados-a-los-".$nfl_team_slug; // The slug of the group, it works
        $group_id = BP_Groups_Group::group_exists($group_slug); // The ID of the group, it works    
        groups_accept_invite( $bp_user_id, $group_id); // Adding the member to the group, don't know if it works
    }
    
    add_action( 'bp_core_activated_user', 'add_members_to_nfl_group', 10, 3 ); //Calling the function on activation of new account, don't know if it works

    I have been reading all threads about adding new members to groups automatically in BP, and from what I have been reading, the code above should work, but it doesn’t and I dont know what is wrong with it. I have been searching and reading for hours and nothing seems to work.

    I don’t like using plugins and I’am learning BP, so, please suggest code solutions.

    Any ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • @laloptk

    Participant

    The file is bp-custom.php, finger error above.

    @henrywright

    Moderator

    Instead of groups_accept_invite(), try using groups_join_group().

    So in your code above, you’d use: groups_join_group( $group_id, $bp_user_id )

    @laloptk

    Participant

    I tried that @henrywright, but no luck. I’m activating test accounts from WP backend manually, maybe that has something to do with the code not adding members to groups. I don’t know if there’s a difference for ‘bp_core_activated_user’ if the admin activates the account from backend or if the member activates the account from their e-mail.

    Right now I can’t send e-mails from my localhost and can’t test if activating by e-mail works.

    @laloptk

    Participant

    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Programatically add new members to a group on account activation’ is closed to new replies.
Skip to toolbar