I have a custom sign up form in my WordPress site which has confirmation email functionality for activating the account. For the confirmation step am keeping another DB table where am keeping the pending users info and when they are going with confirmation link then am copying the info from that table and adding to wp_users table. The issue is that i have First Name and Surname fields which info is being kept in another Wp table called wp_usermeta.
So my question is how i can insert the corresponding user firstname, surname when am adding the user to wp_users after confirmation like this
$checkUserID = $wpdb->get_results("SELECT * FROM pendingwpusers WHERE token = '".$gettokenval."'");
$checkUserIDMain = $wpdb->query("SELECT * FROM store_users WHERE TrackNumber="".$gettokenval.""");
//$aaaa = mysql_num_rows($checkUserIDMain);
//var_dump($checkUserIDMain);
if($checkUserID && $checkUserIDMain == 0){
foreach ($checkUserID as $checkUser) {
//if(wp_mail($to, $subject, $message, $header)){}else{mail($to, $subject, $message, $header);}
$hashedpass = md5($checkUser->user_pass);
$wpdb->insert(
'store_users',
array(
'user_login' => $checkUser->user_login,
'user_pass' => $hashedpass,
'user_nicename' => $checkUser->user_nicename,
'user_email' => $checkUser->user_email,
'user_registered' => $checkUser->user_registered,
'display_name' => $checkUser->display_name,
'TrackNumber' => $checkUser->token
),
array(
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s'
)
);
}
}
2 Answers
If you have the ID of the user you can do this:
wp_update_user([
'ID' => $userId, // this is the ID of the user you want to update.
'first_name' => $firstName,
'last_name' => $lastName,
]);
You can update / insert almost all fields with this function. Take a look at the documentation here