profiles - Is possible to modify user_login after registration?

I would like to give the user the possibility to update his profile via a profile.php page.But I noticed that the field

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 badges
Add a comment  | 

1 Answer 1

Reset to default -1

WordPress 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

相关推荐

  • profiles - Is possible to modify user_login after registration?

    I would like to give the user the possibility to update his profile via a profile.php page.But I noticed that the field

    5小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信