I am writing a module where I need to create a new user and save his profile fields using Profile 2.

This is the code I'm using, but it's not working:

$edit = array(
  'name' => $submission->data[1]['value'][0],
  'pass' => user_password(),
  'mail' => $email,
  'init' => $email,
  'status' => 1,
  'roles' => $role_id,
  'profile_first_name' => $submission->data[1]['value'][0],
  'profile_surname' => 'ag',
  'profile_email' => $email,
  'access' => REQUEST_TIME,

$new_user = user_save(drupal_anonymous_user(), $edit/*,'customer_profile'*/);

What am I doing wrong?

  • It appears someone else decided to rewrite your question to something completely different than what you originally asked, so I apologize for answering to that question instead of what you really wanted to know. – user7 Dec 28 '11 at 10:35

I load the profile object:

$profile = profile2_by_uid_load($user->uid, '<role type here>');

Then I edit the $profile object.

I have a Rules component that saves the profile object. I forget why I have the rule but I remember that it requires quite a bit of code and database queries to get it right.

I set the Rules component to take the $profile object as an argument / parameter and invoke it like this.

    rules_invoke_component('rules_machine_name_this_is_the_component_url_too', $profile);
  • - I am creating the user, but when i create the user its automatically login the user. How to prevent this ? – Hacker Dec 29 '11 at 7:57
  • Might be you are using user_load() function, I had also face same issue. – Ajay Gadhavana Mar 14 '17 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.