Support » Plugin: Groups » How to programatically remove a user to a group

  • Resolved mirgcire


    I am working on a site that allows students to enroll in classes. When they enroll in a class, I want to automatically add them to the class group. And if they cancel the enrollment, I want to automatically remove them.

    I found this in the documentation

    Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group_id ) );

    After digging in the source a little, I am guessing that this is the way to remove:

    Groups_User_Group::delete ( $user_id, $group_id ) );

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author itthinx


    I am attempting to use Groups_User_Group::create. But it is not working.
    So, I added some debug messages to this function to see what is going on.

    I have a group called ‘2014Q3-DC0’ and it is visible in the admin panel. When I entered create my error_log message printed the user id and group id correctly, however results of Group_Group::read($group_id) was empty.

    Do you have any suggestions for further analysis?


    I was skimming through your code and found another function called read_by_name.

    I used this to convert the group name into a group id.

    Then (after fixing a couple more bugs) everything magically worked!


    Hi there,

    First thank you and congratulations for the great Groups plugin!

    I have enjoyed using it to protect pages “by hand” for some users through the ultra-simple UI you provide in the WP backend. But I have now to go a step further by doing things programmatically in php templates (at beginner level).

    What I was conveniently doing by hand was, for a set of manually create pages :

    • define both a capability and a group to protect thes pages and assign them to the pages
    • assign users to the group for them (only) being able to read the page

    I now have to do the same in php for a set of page created programmatically. I have seen the code snippets you give in your doc to do the second step (assign users to group), no problem. But I am lost in your API for achieving the first step, which I understand is a list of several API calls that I can’t figure out.

    Would you have some code examples on how to do that, or more precise advices for me to use the API (I’m a php beginner but I can understand things).

    Many thanks in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to programatically remove a user to a group’ is closed to new replies.