I would like to give the user the possibility to update his profile via a profile.php page.
But I noticed that the field in the database marked as user_login is not updated.
I searched around and they say that WordPress does not give the possibility to modify it, is it true?
I am currently using this code to update the various user fields:
//Sanitize POST Array
$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);
$nome = $POST['nome'];
$cognome = $POST['cognome'];
$user_login = $nome . ' ' . $cognome;
$user_data = array(
'ID' => $current_user->ID,
'user_login' => $user_login,
'display_name' => $user_login,
'first_name' => $nome,
'last_name' => $cognome,
);
$user_id = wp_update_user($user_data);
Thanks to those who will help me.
I would like to give the user the possibility to update his profile via a profile.php page.
But I noticed that the field in the database marked as user_login is not updated.
I searched around and they say that WordPress does not give the possibility to modify it, is it true?
I am currently using this code to update the various user fields:
//Sanitize POST Array
$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);
$nome = $POST['nome'];
$cognome = $POST['cognome'];
$user_login = $nome . ' ' . $cognome;
$user_data = array(
'ID' => $current_user->ID,
'user_login' => $user_login,
'display_name' => $user_login,
'first_name' => $nome,
'last_name' => $cognome,
);
$user_id = wp_update_user($user_data);
Thanks to those who will help me.
Share Improve this question asked Sep 5, 2019 at 14:09 Matteo FeduziMatteo Feduzi 291 silver badge9 bronze badges1 Answer
Reset to default -1WordPress does not allow updating user_login
for existing users. You may update it using $wpdb->update
as below:
// Sanitizes the username
$user_login = sanitize_user( trim( $user_login ), true );
// Check if the given username exists
if ( ! empty( $user_login ) && ! username_exists( $user_login ) ) {
global $wpdb;
//Update user record
$wpdb->update( $wpdb->users, array('user_login' => $user_login), array('ID' => $user_id));
// Delete cached user objects
wp_cache_delete( $user_id, 'users' );
wp_cache_delete( $user_login, 'userlogins' );
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745182540a4615485.html
评论列表(0条)