Programmatically assign new users to OG My site is sub-divided into organic groups. 

Group administrators are allowed to create new users, but I need a way to auto-assign the new user to the same organic group the creating group administrator belongs to.

Can anyone tell me how I can accomplish this auto-assignment either programmatically or with a module?
You should write a custom module that implements hook_form_alter().


Get the $form_id of the 'create new user' form that you want to alter. 

( drupal_set_message($form_id); is an easy way to do this. )
Add a custom submit handler to the 'create user' form that uses the respective details and the og_group() function to add the new user to the group.

function MODULENAME_form_alter(&amp;$form, &amp;$form_state, $form_id) {

    if (isset($form_id) &amp;&amp; $form_id == 'YOUROG_USERCREATION_FORMID') {
        $form['#submit'][] = 'MODULENAME_submit_handler';
    }

}


function MODULENAME_submit_handler($form, &amp;$form_state) {

    $gid = $form_state['values']['groupID'];
    $uid = $form_state['values']['userID'];

    $account = user_load($uid);
    $values = array(
        'entity_type' =&gt; 'user',
        'entity' =&gt; $account,
        'state' =&gt; OG_STATE_ACTIVE,
       );

    og_group('node', $gid, $values);

    return $form;
}



Just as a point of information, this code sample is geared towards OG 7.2.x.

If you're using OG 7.1.x, you drop the first argument in og_group, so it would be og_group($gid, $values);

Also, the possible $values you can use, and defaults, are as follows: 

# taken directly from og.module

$values += array(
     'entity type' =&gt; 'user',
     'entity' =&gt; FALSE,
     'state' =&gt; OG_STATE_ACTIVE,
     'save' =&gt; TRUE,
     'force reload' =&gt; TRUE,
     'membership type' =&gt; OG_MEMBERSHIP_TYPE_DEFAULT,
     'membership fields' =&gt; array(),
     'created' =&gt; time(),
    );