skip to Main Content

Where does form submission data go from wordpress wp_insert_user go if there is not already usermeta field names that match?

Specifically, I included Company Name and Phone as fields in the form submission, however when I pull the data from the wp_users and wp_usermeta table I do not see these new fields.

Code that handles the wp_insert_user function:

if(empty($errors)){
$new_user_id = wp_insert_user(array(
'user_login'       => $user_login,
'user_pass'        => $user_pass,
'user_email'       => $user_email,
'first_name'       => $user_first,
'last_name'        => $user_last,
'company'          => $user_company,
'user_registered'  => date('Y-m-d H:i:s'),
'role'             => 'subscriber',
));

I tried the above code and expected to see these fields in either the user or usermeta tables, though I did not.

2

Answers


  1. Chosen as BEST ANSWER

    Please reference this documenation for additional details. You are not able to add items outside the specifics of the wp_insert_user() options, though you can specify additional user meta data by using the update_user_meta() function as displayed below.

    I needed to make the initial wp_insert_user(array) call, then using the same $user_id from that call, I could insert the user meta by calling update_user_meta().

    documentation link: https://developer.wordpress.org/plugins/users/working-with-user-metadata/#programmatically

    Code:

    if(empty($errors)){
    $new_user_id = wp_insert_user(array(
    'user_login'       => $user_login,
    'user_pass'        => $user_pass,
    'user_email'       => $user_email,
    'first_name'       => $user_first,
    'last_name'        => $user_last,
    'user_registered'  => date('Y-m-d H:i:s'),
    'role'             => 'subscriber',
    ));
    
    if($new_user_id){
                update_user_meta( $new_user_id, "user_company", $user_company);
                update_user_meta($new_user_id, "user_phone", $user_phone);
            }
    

  2. In this case, you added two new fields "Company Name" and "Phone" to registration form and you are trying to save their values as user metadata, then you need to pass those values as key-value pairs in the $userdata array as per the below code:

    $userData = array(
        'user_login' => $user_login,
        'user_pass' => $user_pass,
        'user_email' => $user_email,
        'first_name' => $user_first,
        'last_name' => $user_last,
        'company' => $company,
        'phone' =>   $phone,
        'user_registered'  => date('Y-m-d H:i:s'),
        'role'             => 'subscriber'
    );
    
    $userId = wp_insert_user( $userData );
    

    After the user creation, you can fetch these fields from the wp_usermeta table using the get_user_meta function in WP. Try below code:

    $company = get_user_meta( $userId, 'company', true );
    $phone = get_user_meta( $userId, 'phone', true );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search