How to display user's nickname by default instead of username

I need to display the user's nickname by default after they are created in the DB, NOT the username.

I am creating a user with:

    wp_create_user( $user_name, $random_password, $user_email );

updating the user nickname field directly after user creation:

    update_user_meta($user_id, 'nickname', $user_nickName);

Now i need the "Display name publicly as" to be set to the nickname as default not the username.

My WP is automatically defaulting the users display name as the username. How do i put the selected value for the display name(Display name publicly as) as my nickname value programmatically?

Topic wp-create-user updates Wordpress

Category Web


Instead of wp_create_user, use wp_insert_user. So you will be able to set extra variables like nickname, display_name separately than the default value.

For your case, you can code like this:

$args = array (
    'user_login'     => $user_name,
    'user_pass'      => $random_password, //send as plain text password string
    'user_email'     => $user_email,
    'nickname'       => $user_nickName,
    'display_name'   => $user_nickName
);

wp_insert_user( $args); //on success, it will return user id

Here is the codex refernece for wp_insert_user

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.