Title: Auto-add users to groups
Last modified: August 22, 2016

---

# Auto-add users to groups

 *  Resolved [iamarogue](https://wordpress.org/support/users/iamarogue/)
 * (@iamarogue)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/auto-add-users-to-groups/)
 * Hey there,
 * I have a question that’s already partially being answered, but I’m still having
   trouble implementing it.
 * [https://wordpress.org/support/topic/automatically-join-users-to-group-at-registration?replies=2](https://wordpress.org/support/topic/automatically-join-users-to-group-at-registration?replies=2)
 * I’m using Sensei, a course management software. Ideally, what I would like is
   to award membership to a group once a course is complete. Basically so I can 
   send newsletters to past students.
 * Anyways, I know that’s a bit more complex, so right now what I’m just trying 
   to do is, when they go to a particular page – i.e. the last lesson in a course–
   I want to automatically add them to the group. I’m trying to use the following
   code:
 * `<?php Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' =>
   $group_id ) ); ?>`
 * But for some reason, nothing’s happening. I’m not very good at php, so I’m clearly
   doing something wrong, but I don’t know what. Any help would be great!
 * **1. Assigning to user?**
    Maybe I’m not assigning to the right user? Is this
   fine: `'user_id' => $user_id` or do i need to add `$user_ID = get_current_user_id();`
   beforehand?
 * **2. Assigning to group?**
    I’m not sure where to put the group ID. The group
   I want is #2. I’ve tried: `'group_id' => '2'` and `'2' => $group_id` and `'group_id'
   => $group->'2'` I’m not sure what the correct thing should be?
 * **3. Placement?**
    Finally, I’m not sure where I should be putting the code. 
   Since I only want to trigger it when you complete a lesson, i put it on the lesson
   itself (using a plugin that allows php in posts). but that’s probably not the
   best place for it. It should probably go in functions.php, but I’m still kind
   of fuzzy on it.
 * By the way, sensei has um hooks? that go like: $course_completed and $course_started
   and $course_ID. So maybe it would be better if I build a code for functions.php
   that looks like:
 * `if course_ID = x and $course_completed = true, then add to group y`
 * Any help with what I’m trying to accomplish would be very appreciated!
 * [https://wordpress.org/plugins/groups/](https://wordpress.org/plugins/groups/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [iamarogue](https://wordpress.org/support/users/iamarogue/)
 * (@iamarogue)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/auto-add-users-to-groups/#post-5642543)
 * Anyone able to suggest assistance?
 *  [gtsiokos](https://wordpress.org/support/users/gtsiokos/)
 * (@gtsiokos)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/auto-add-users-to-groups/#post-5642602)
 * Hi iamarogue,
 * You can use this function so when the user that belongs to a group and visits
   a specific post, switches to another group.
 *     ```
       function gt_add_user_to_group () {
       	$current_user_id = get_current_user_id();
       	$is_a_member = false;
       	require_once( ABSPATH . 'wp-includes/pluggable.php' );
       	if ( $group = Groups_Group::read_by_name( 'Red Members' ) ) {
       		$is_a_member = Groups_User_Group::read( $current_user_id , $group->group_id );
       }
       	if ( is_single(89) && $is_a_member ) {
       		Groups_User_Group::delete( $current_user_id, 2 );
       		Groups_User_Group::create( array( 'user_id' => $current_user_id, 'group_id' => 3 ) );
       	}
       }
       add_action( 'pre_get_posts', 'gt_add_user_to_group' );
       ```
   
 * put it in your child theme’s functions.php.
    You should replace 89 with your 
   desired post id 2 and 3 with your desired group ids of course.
 * Although pre_get_posts is not the best hook for your case, because the hook gets
   triggered even if the user visits the post without completing the course, i can’t
   think of something better.
 * Cheers

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Auto-add users to groups’ is closed to new replies.

 * ![](https://ps.w.org/groups/assets/icon-256x256.png?rev=983146)
 * [Groups - Memberships and Access Control](https://wordpress.org/plugins/groups/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/groups/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/groups/)
 * [Active Topics](https://wordpress.org/support/plugin/groups/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/groups/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/groups/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [gtsiokos](https://wordpress.org/support/users/gtsiokos/)
 * Last activity: [11 years, 3 months ago](https://wordpress.org/support/topic/auto-add-users-to-groups/#post-5642602)
 * Status: resolved