Forcing nickname as display_name in custom edit profile template

I’m not 100% sure if I follow the logic of your question. But this is probably what you need:

if ( !empty( $_POST['nickname'] ) ) {
    wp_update_user( array ('ID' => $current_user->id, 'display_name' => esc_attr( $_POST['nickname'] ) ) ) ;
    update_user_meta($current_user->id, 'nickname', esc_attr( $_POST['nickname'] ) );
    update_user_meta($current_user->id, 'display_name', esc_attr( $_POST['nickname'] ) );
}

Attention: update_usermeta has been deprecated and you should use update_user_meta.

Also, I really don’t know why there is a display_name in both tables, wp_users and wp_users_meta, but I guess this deserves a question of its own.

I believe this lines in your code are for testing or some left over, as I can’t see their usefulness and the $current_user/$user_id part is plainly wrong…

global $wpdb;

global $current_user;
$current_user = wp_get_current_user();

$user_id = wp_get_current_user();
$nickname="testname";
update_user_meta($user_id, 'nickname', $nickname);

Leave a Comment