I have been at this for almost two hours, i'm sure i'm close, but can't find the nudge to make it work. I built a module from scratch to import users from a csv file, the user creation is working fine, and i'm happy with that.
For the process to be convenient, i want to be able to update the users too, and always work with the same csv file. The wp_update_user does nothing, no error, but values from array are not updated. update_user_meta does not work either. i suppose it is the way i return the $user_id, since the ID is not included in the array...
$userdata = array(
'user_login' => $username,
'nickname' => $username,
'user_nicename' => $user_nicename,
'user_email' => $user_email,
'first_name' => $prenom,
'last_name' => $nom,
'user_registered' => date( 'Y-m-d H:i:s' ),
'display_name' => $username,
'show_admin_bar_front' => false,
'role' => 'subscriber',
'admin_color' => "fresh",
'rich_editing' => "true",
);
// check if user exists
if (username_exists($username)) {
$user_id = $userdata[0]->ID;
$user_id = wp_update_user( $userdata );
update_user_meta( $user_id, 'region', $region );
} else {
// create new user
$user_id = wp_insert_user($userdata);
wp_new_user_notification( $user_id, null, 'both' );
update_user_meta( $user_id, 'region', $region );
}
Edit : So here is the fix, in case anyone is looking for a similar issue :
if (username_exists($username)) {
$the_user = get_user_by('login', $username);
$user_id = $the_user->ID;
if (!is_wp_error( $user_id )){
$push_id = array('ID' => $user_id);
$merge = array_merge($userdata, $push_id);
$user_id = wp_update_user( $merge );
update_user_meta( $user_id, 'region', $region );
}
else {
$html_update = "Broken";
}
} else {
// create new user
$user_id = wp_insert_user($userdata);
wp_new_user_notification( $user_id, null, 'both' );
update_user_meta( $user_id, 'region', $region );
}
Thank you birgire, it helped me find a solution
I have been at this for almost two hours, i'm sure i'm close, but can't find the nudge to make it work. I built a module from scratch to import users from a csv file, the user creation is working fine, and i'm happy with that.
For the process to be convenient, i want to be able to update the users too, and always work with the same csv file. The wp_update_user does nothing, no error, but values from array are not updated. update_user_meta does not work either. i suppose it is the way i return the $user_id, since the ID is not included in the array...
$userdata = array(
'user_login' => $username,
'nickname' => $username,
'user_nicename' => $user_nicename,
'user_email' => $user_email,
'first_name' => $prenom,
'last_name' => $nom,
'user_registered' => date( 'Y-m-d H:i:s' ),
'display_name' => $username,
'show_admin_bar_front' => false,
'role' => 'subscriber',
'admin_color' => "fresh",
'rich_editing' => "true",
);
// check if user exists
if (username_exists($username)) {
$user_id = $userdata[0]->ID;
$user_id = wp_update_user( $userdata );
update_user_meta( $user_id, 'region', $region );
} else {
// create new user
$user_id = wp_insert_user($userdata);
wp_new_user_notification( $user_id, null, 'both' );
update_user_meta( $user_id, 'region', $region );
}
Edit : So here is the fix, in case anyone is looking for a similar issue :
if (username_exists($username)) {
$the_user = get_user_by('login', $username);
$user_id = $the_user->ID;
if (!is_wp_error( $user_id )){
$push_id = array('ID' => $user_id);
$merge = array_merge($userdata, $push_id);
$user_id = wp_update_user( $merge );
update_user_meta( $user_id, 'region', $region );
}
else {
$html_update = "Broken";
}
} else {
// create new user
$user_id = wp_insert_user($userdata);
wp_new_user_notification( $user_id, null, 'both' );
update_user_meta( $user_id, 'region', $region );
}
Thank you birgire, it helped me find a solution
When updating a user with:
$user_id = wp_update_user( $userdata );
you need to have the user ID set within $userdata
.
Also make sure to validate the $user_id
output of wp_update_user()
as it can be either an integer or an instance of WP_Error
, before using it in update_user_meta()
.
One can use is_wp_error( $user_id )
to check if $user_id
is an instance of the WP_Error
class.
PS:
Make sure to have a look at PHP errors while developing.
When the new fatal error protection (WSOD) kicks in after 5.1, one might need to disable it by defining the constant:
WP_DISABLE_FATAL_ERROR_HANDLER
as true
in wp-config.php
on dev installs.