Skip to:
Content
Pages
Categories
Search
Top
Bottom

Add user to group automatically


  • Carlen
    Participant

    @carlen

    I have seen what is posted out there and everyone suggests working off of a few plugins developed years back. This is the code I constructed after looking at what they did, but it still doesn’t work.

    I can pass the ID of the group a user selects through an entry, but still can’t get the auto join to group working. Any suggestions?

    function ip_gravity_registration_group( $user_id, $feed, $entry ) {
        global $bp, $wpdb;
    	$selected_team = rgar( $entry, '13' );
    	$group_id = $selected_team;
    	$user_id  = new WP_User( $user_id );
    	groups_join_group($group_id, $user_id);
    }
    add_action( 'gform_user_registered', 'ip_gravity_registration_group', 10, 4 );
Viewing 2 replies - 1 through 2 (of 2 total)

  • clickallco
    Participant

    @clickallco

    This should work

    add_action( 'bp_core_activated_user', 'join_group_on_signup');
    function join_group_on_signup( $user_id ){
        $group_id = 13; //change it to the group ID of your choice
        groups_join_group( $group_id, $user_id );
    }

    Carlen
    Participant

    @carlen

    Thank you! this did help me realize where my errors were, but wasn’t working because I am using some code of automatic login. For anyone else this is what I came up with that works.

    I set up https://gravitywiz.com/documentation/gravity-forms-populate-anything/ and grab the groups from the wp database. I have them search and select the name, and then pass the value (ID) this to a hidden field.

    I then set up the following code. Included is some code to automatically log them in after they register too.

    function ip_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) {
       $selected_team = rgar( $entry, '13' ); // Replace with the field for group ID	$group_id = $selected_team; //change it to the group ID of your choice
        groups_join_group( $group_id, $user_id );
    // Automatic Login: Only automatically login if we aren't *already* logged in
    if ( ! is_user_logged_in() ) { 
    // Get the user data (for the login)
       $user = get_userdata( $user_id );
    // Sign the user in
    wp_signon( array(
    'user_login' => $user->user_login,
    'user_password' => $password,
    'remember' => false, // Don't set the remember cookie
    ) );
    }
    }
    add_action( 'gform_user_registered', 'ip_gravity_registration_autologin', 10, 4 );
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar