• Resolved teowim

    (@teowim)


    I’m using the “Groups” plugin for my WordPress site. I would like to automatically add a user to a group if their email ends with “@xxxx.com”.

    Currently, I am trying to add all users to this group because I have not yet implemented the email check.

    Here is the code I am using:

    add_action( 'user_register', 'add_user_to_group', 10, 1 );
    function add_user_to_group( $user_id ) {
            do_shortcode('[groups_join group='Prenium']');
    }
    

    However, it appears that this code is not functioning as expected. When I create an account, the user is not being added to the correct group. He is added to the “registerd” group, who is the default group.

    I’m using WordPress version 6.3.1, and I’m certain that the group name is correct, and the shortcode seems to exist.

    If anyone has any ideas about why this is not working, I would greatly appreciate your input.

    Thank you for your assistance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter teowim

    (@teowim)

    If someone need to do the same thing one day, there is how to do it :

    add_action( 'user_register', 'group_registration_save', 10, 1 );
    
    function group_registration_save($user_id) {
        if (isset( $_POST['email'] )) {
            $email = $_POST['email'];
            $domain = substr(strrchr($email, "@"), 1);
    
        if ($domain === 'xxx.com' || $domain === 'xxx.fr') {
            $group = Groups_Group::read_by_name('Premium');
            if ( $group ) {
                Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group->group_id));
            }
        }
    }
    Plugin Author Kento

    (@proaktion)

    Thanks for sharing your solution!

    As a reference for others who might seek a similar solution, there is also this plugin which adds a user on registration based on his role https://github.com/itthinx/groups-role-registration

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add user automaticly to a group on registration’ is closed to new replies.