0

i am trying to automatically group users when they register according to a list of selections in the register form. I am using WP User Group plugin to make groups and Ultimate Member to register users.

I didn't manage to figure out a working solutions for my issue. Can anyone help me realize what i am missing.

add_action( 'user_register', 'AutoGroup', 10, 1 );

function AutoGroup($user_id){
    $user_args = get_user_meta($user_id,'activity_domain' , true);
        if ($user_args == 'Public Services'){
            wp_set_object_terms($user_id, 61, 'user-group', FALSE);
        }
        else if ( $user_args == 'Private Services'){
            wp_set_object_terms($user_id, 62, 'user-group', FALSE); 
        }
        else{
            wp_set_object_terms($user_id, 63, 'user-group', FALSE);
        }
}

Later edit: I have 3 groups: "Public Services", "Private Services" and "Others" the problem i am facing is that all user who register on the site automatically goes into the "Other" group, so i am guessing that "$user_args" variable doesn't take any value. I have also tried this code but no luck.

add_action( 'user_register', 'AutoGroup', 10, 1 );
function AutoGroup($user_id){
$user = new WP_User($user_id);
foreach ($user->activity_domain as $user_args){
    if ($user_args == 'Public Services'){
        wp_set_object_terms($user_id, 61, 'user-group', FALSE);
    }
    else if ( $user_args == 'Private Services'){
        wp_set_object_terms($user_id, 62, 'user-group', FALSE); 
    }
    else{
        wp_set_object_terms($user_id, 63, 'user-group', FALSE);
    }
}

I dont know what i am missing. I dont know exactly how to debug my code and see in real time how the variable takes values but i have made a shortcode with the exact same code and when i run the shortcode on a page it works perfectly, and when i say perfectly i mean that it query the database for the logged user and displays his meta_value .

I have to specify that i am now trying to learn PHP and Wordpress and i really count on your help.

4
  • When you say it doesn't work, what doesn't work? Is there an error? Commented Sep 27, 2022 at 15:30
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Sep 27, 2022 at 15:30
  • I have provided additional details in the answer bellow.
    – Victor1290
    Commented Sep 28, 2022 at 5:59
  • @Victor1290 do not post replies as the solution to your question, I have deleted the answer but the site would now consider your question to have had answers. If you have new information for your question, you must edit it into the question using the Edit link underneath the tags. The last thing you want is for people to think you've already gotten a solution
    – Tom J Nowell
    Commented Sep 28, 2022 at 8:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.