<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:arial, helvetica, sans-serif;font-size:12pt"><DIV></DIV>
<DIV>You don't need to load the whole user object to do this - way too much potential overhead.</DIV>
<DIV> </DIV>
<DIV><FONT face="Courier New, courier, monaco, monospace, sans-serif"> $uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $name_you_just_got));</FONT></DIV>
<DIV><FONT face="Courier New, courier, monaco, monospace, sans-serif"> db_query("INSERT INTO {users_roles} (uid, rid) VALUES(%d, %d)", $uid, $rid_of_role_to_add);<BR></FONT> </DIV>
<P><FONT face="bookman old style, new york, times, serif" color=#ff007f size=4><EM><STRONG>Nancy E. Wichmann, PMP</STRONG></EM></FONT></P>
<P><FONT face="arial, helvetica, sans-serif">Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.</FONT></P>
<DIV><BR></DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: arial, helvetica, sans-serif"><BR>
<DIV style="FONT-SIZE: 10pt; FONT-FAMILY: arial, helvetica, sans-serif"><FONT face=Tahoma size=2>
<HR SIZE=1>
<B><SPAN style="FONT-WEIGHT: bold">From:</SPAN></B> Steve Edwards <killshot91@gmail.com><BR><B><SPAN style="FONT-WEIGHT: bold">To:</SPAN></B> Drupal Development <development@drupal.org><BR><B><SPAN style="FONT-WEIGHT: bold">Sent:</SPAN></B> Fri, February 26, 2010 4:49:02 PM<BR><B><SPAN style="FONT-WEIGHT: bold">Subject:</SPAN></B> [development] How to add user role during registration with custom form<BR></FONT><BR>I took over a site that uses a custom registration form that directly calls user_register_submit directly, and I need to have a user role added to this user when this form is submitted. However, I can't add the value to $form_state['values']['roles'] because the function errors out if there is a roles array because it is seen as a 'malicious attempt to alter protected user fields.' Since that function then calls user_save, it seems that once the user is saved, I need to 1) load the user I just created, 2) add the role
to the object, and 3) save it again. However, in order to call user_load, I need the uid, which isn't returned from user_register_submit. The only logical thing I can think to do is query the users table with the user name I just created (entered as part of the form), get the rid, and then call user_load and do my thing.<BR><BR>Is there a better (and easier) way to do this that I'm missing, or am I on the right track?<BR><BR>Thanks.<BR><BR>Steve</DIV></DIV></div></body></html>