how to get wordpress user id when user register
-
26-04-2021 - |
Question
im creating wordpress user using wp_create_user()
function and when user creating i want to add that users userid to custom table,
this is the way i tried
add_action( 'user_register', 'atuser_info');
function atuser_info( $user_id ) {
global $wpdb;
//$prefix=$wpdb->prefix;
$query_userinfo = "INSERT INTO user_info (id,fname,lname,address_1) VALUES (".$user_id.",".$_POST['fname'].",".$_POST['lname'].",".$_POST['address_1_step2'].")";
$wpdb->query($query_userinfo);
}
Solution
If wp_create_user
executes successfully, it will return the id of the created user. [Codex]
Here it is in action with an error handler.
$user_id = wp_create_user();
if( is_wp_error($user_id) ) {
echo $user_id->get_error_message();
} else {
//add into custom table
echo $user_id;
}
OTHER TIPS
// $user_login is the username
// $user_email is the email address
$user_id = register_new_user($user_login, $user_email);
if( $user_id ) {
echo $user_id;
// add to your database
}
else {
echo $user_id->get_error_message();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange