Skip to:
Content
Pages
Categories
Search
Top
Bottom

add user to group when user is added via dashboard


  • anrahulpandey
    Participant

    @anrahulpandey

    Hi,

    I have created a group and want to add user to it automatically whenever the admin create the new user from WordPress user dashboard. I am using the following code snippet –

    function automatic_group_membership_bp($user_id)
    {
        //if no user id return
        if (!$user_id) {
            return false;
        }
    
        // 1 is the group id
        $group_id = 1;
        //Get user meta to find the role
        $user_meta = get_userdata($user_id);
        $user_roles = $user_meta->roles;
        //only add user to group if it subscriber
        if (in_array("subscriber", $user_roles)) {
            $update_bp_user_last_activity = bp_update_user_last_activity($user_id, date("Y-m-d H:i:s"));
            //Buddypress hook to add member to a group
            $status = groups_join_group($user_id, $group_id);
        }
    
    }
    
    add_action('user_register', 'automatic_group_membership_bp', 10, 3);

    I am adding a user with role subscriber to group. $status returns 1 that means the user is getting added to the group. I can even see the user in http://siteurl/members list. But I cannot see the user in a group. I am not sure what I am missing here, can you please help me out.

Viewing 1 replies (of 1 total)

  • Paul Gibbs
    Keymaster

    @djpaul

    @anrahulpandey Check the function declaration for groups_join_group() inside BuddyPress’ files!

    function groups_join_group( $group_id, $user_id = 0 )

    I think you are specifying the user_id and group_id parameters in the wrong order. 🙂

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar