Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8985379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:19:01+00:00 2026-06-15T21:19:01+00:00

How can i assign newly created user to the particular group in modx Programmaticaly

  • 0

How can i assign newly created user to the particular group in modx Programmaticaly ? Below is my code

if(isset($_POST) && count($_POST)){
  $oUser = $modx->newObject('modUser');
  $oUser->set('username', "test");
  //$oUser->set('password', "test");

  $oProfile = $modx->newObject('modUserProfile');
  $oProfile->set('fullname', $_POST['fname']);
  $oProfile->set('email', $_POST['email']);
  $oUser->addOne($oProfile);
  if($oUser->save()===false){
    echo "Error";
  }else
    echo "Done";
}

I googled but all i find is graphical tutorial how to create groups and edit user and then assign roles, If you know any tutorial then also its fine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:19:02+00:00Added an answer on June 15, 2026 at 9:19 pm

    Here is how I have been doing it, this is a posthook snippet that fires after a user registers [and the user is created]

    <?php
    $specialty = $hook->getValue('specialty');
    $country =  strtolower($hook->getValue('excountry'));
    $username = $hook->getValue('username');
    $staff = $hook->getValue('staff-or-resident'); //Staff | Resident
    
    $joingroup = '';
    $joinrole = '';
    $blockuser = 'false';
    
    switch ($specialty){
    
        case 'Other' :
            $joingroup = 15; // Other 
            $joinrole = 1; //member
            $blockuser = 'true';
            break;
    
        // there are about 15 different groups and roles here... 
    
        default :
            $joingroup = '0'; // none
            $joinrole = '0'; // none 
            break;  
    }
    
    if($joingroup > 0 && $joinrole > 0){
        $user = $modx->getObject('modUser', array('username'=>$username));
        $internalkey = $user->get('id');
        $profile = $user->getOne('Profile',array('internalKey'=>$internalkey));
    
        $user->joinGroup($joingroup, $joinrole);
    
        if($blockuser == 'true'){ //block user if they belong to the "other" group
            $profile->set('blocked',1);
        }
    
        if(!$user->save()){
            return false;
        };
    }
    
    return true;
    

    The key is the: $user->joinGroup($joingroup, $joinrole); where joingroup is the group id ~ or name and the joinrole is the role id ~ or name. It’s documented here: http://api.modx.com/revolution/2.1/_model_modx_moduser.class.html#%5CmodUser::joinGroup()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I assign query result into array elements? This is my code: include('db.php');
I need newly created users to be assigned to a particular role as default.
I am currently working on a custom cms where user can assign multiple categories
I have a project object. A user can assign a worker object for it.
A user can assign a field due_time_at which has the TIME datatype in the
Actually, I can assign a task to a user in the edition task. I
Can anyone tell me how I can assign a click function to particular coordinates
Please help me: create clickable regions in the canvas below that I can assign
I can assign a tuple as follows: var (min, max) = (1, 2) But
From report manager we can assign new roles , but these users needs to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.