custom field - adding extra wordpress user info from registration form

admin2025-06-04  2

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' );

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' );
Share Improve this question edited Jan 24, 2019 at 6:16 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Jan 24, 2019 at 5:24 SpeedySpeedy 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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' );

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748980235a315348.html

最新回复(0)