Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Change Default Display Name Publicy As for all existing users

Tags:

wordpress

does anyone know of way I can change the existing display name publicly as for all users. I want to have firstname lastname as the default because this will reflect in the forum. I have read all the forums and have tried all the hacks, any suggestions would be appreciated. Thanks in advance

like image 945
uknowit2 Avatar asked Feb 17 '12 10:02

uknowit2


People also ask

How do I change the default display name publicly as WordPress users?

The Default – Display name publicly as field only appears on the Edit Profile Page. Inside the field options you can set the field title, description and default value. Also you can set it be Required or not.

What is WordPress display name?

You can change both your WordPress.com account username (the name you use to login) and your display name (the name that is seen on your posts and comments).

Can usernames be changed in WordPress?

To actually change WordPress usernames, you'll need to press Update for the specific user you want to edit. Next, write your New Username and choose if you want to send an email notification about the changes just made. Then press Update Username and that's it, you are good to go.

How do I show users name in WordPress?

To get a WordPress user's display name, you need to get the user object, then use $user->display_name to access the user's name. If the user hasn't specifically set anything as their display name, it will default to their username. So you don't have to worry about fallbacks.


2 Answers

Here's an improved version of richplane's answer that works in newer versions of WordPress (3.8+):

/**
* Format WordPress User's "Display Name" to Full Name on Login
* ------------------------------------------------------------------------------
*/

add_action( 'wp_login', 'wpse_9326315_format_user_display_name_on_login' );

function wpse_9326315_format_user_display_name_on_login( $username ) {
    $user = get_user_by( 'login', $username );

    $first_name = get_user_meta( $user->ID, 'first_name', true );
    $last_name = get_user_meta( $user->ID, 'last_name', true );

    $full_name = trim( $first_name . ' ' . $last_name );

    if ( ! empty( $full_name ) && ( $user->data->display_name != $full_name ) ) {
        $userdata = array(
            'ID' => $user->ID,
            'display_name' => $full_name,
        );

        wp_update_user( $userdata );
    }
}
like image 74
rjb Avatar answered Sep 19 '22 05:09

rjb


Problem with using the admin_head hook is that it doesn't work for users who don't use the admin system. Also, my attempts to implement the solution posted by Marty failed because it doesn't seem that the display_name can be updated by update_user_meta() - you have to use wp_update_user().

My proposal - put this in your functions.php file:

function force_pretty_displaynames($user_login, $user) {

    $outcome = trim(get_user_meta($user->ID, 'first_name', true) . " " . get_user_meta($user->ID, 'last_name', true));
    if (!empty($outcome) && ($user->data->display_name!=$outcome)) {
        wp_update_user( array ('ID' => $user->ID, 'display_name' => $outcome));    
    }
}
add_action('wp_login','force_pretty_displaynames',10,2); 

For me (using WP 3.4.1), that works OK, replacing the display name as soon as they log in.

like image 23
richplane Avatar answered Sep 22 '22 05:09

richplane