1

I am trying to add some extra user profile information from a wordpress registration form. But only the standard wordpress user info is being added. Can this work just using wp_insert_user or do I need to add an extra function for wp_update_user ?

thank you. ( I am very much a beginner at coding, so sorry if this is obvious)

    function crm_wp_insert_user() {
      if ( isset($_POST['crm_customer_submitted'] ) ) {

      $firstname   =   sanitize_user( $_POST['firstname'] );
      $lastname   =   sanitize_user( $_POST['lastname'] );
      $project   =   sanitize_user( $_POST['project'] );
      $email      =   sanitize_email( $_POST['email'] );
      $phone      =   sanitize_user( $_POST['phone'] );   
      $address      =   sanitize_user( $_POST['address'] );       


    $user_data = array(
        //'ID' => '',
        'user_pass' => wp_generate_password(),
        'user_login' => $email,
        'user_nicename' => $firstname,
        'user_url' => '',
        'user_email' => $email,
        'display_name' => $firstname,
        'nickname' => $firstname,
        'first_name' => $firstname,
        'last_name' => $lastname,
        'description' => $project,
        'phone' => $phone,  
        'address' => $address,      
        'user_registered' => $date,
        'role' => crm_client // Use default role or another role, e.g. 'editor'
    );
    $user_id = wp_insert_user( $user_data );
      }

}
add_action( 'admin_init', 'crm_wp_insert_user' );
0

You can use usermeta wordpress function to add extra information for user, below code will help you


function crm_wp_insert_user() {
  if ( isset($_POST['crm_customer_submitted'] ) ) {

  $firstname   =   sanitize_user( $_POST['firstname'] );
  $lastname   =   sanitize_user( $_POST['lastname'] );
  $project   =   sanitize_user( $_POST['project'] );
  $email      =   sanitize_email( $_POST['email'] );
  $phone      =   sanitize_user( $_POST['phone'] );   
  $address      =   sanitize_user( $_POST['address'] );       


$user_data = array(
    //'ID' => '',
    'user_pass' => wp_generate_password(),
    'user_login' => $email,
    'user_nicename' => $firstname,
    'user_url' => '',
    'user_email' => $email,
    'display_name' => $firstname,
    'nickname' => $firstname,
    'first_name' => $firstname,
    'last_name' => $lastname,    
    'user_registered' => $date,
    'role' => crm_client // Use default role or another role, e.g. 'editor'
);
$user_id = wp_insert_user( $user_data );

//Below function will add extra details to user
add_user_meta($user_id,'description',$project);
add_user_meta($user_id,'phone',$phone);
add_user_meta($user_id,'address',$address);

  }
} add_action( 'admin_init', 'crm_wp_insert_user' );

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.