WordPress dashboard offers user the opportunity to choose whether their name is output as Firstname Lastname or several other options with Firstname Lastname order the default.
I would like that default changed to Lastname Firstname.
Can anyone suggest how it can be done?
1 Answer
AFAIK the default option in the display name dropdown is set to the current value of user display name.
So, if we on user creation set display_name
to “Lastname Firstname”, then this will be used as default.
This can be done hooking user_register
:
add_action('user_register', 'last_name_first');
function last_name_first( $uid ) {
remove_action('user_register', 'last_name_first');
$user = new WP_User($uid);
$first = $user->first_name;
$last = $user->last_name;
$full = trim("{$last} {$first}");
if ( ! empty( $full ) ) {
global $wpdb;
$wpdb->update(
$wpdb->users,
array( 'display_name' => $full ), array( 'ID' => $uid ),
array('%s'), array('%d')
);
}
However, that will affect the name shown on frontend, on backend, in list table of users, under “Name” column, we’ll still see “Firstname Lastname”.
That can be changes, using the filter ‘manage_users_columns’ and ‘manage_users_custom_column’, in a way that we replace the default ‘name’ column with a custom one showing the display_name
field:
add_filter('manage_users_columns', 'filter_users_colums', 9999);
function filter_users_colums( $cols ) {
if ( isset($cols['name']) ) {
$old = $cols;
$cb = $old['cb'];
$un = $old['username'];
unset( $old['cb'], $old['username'], $old['name'] );
$cols = array_merge( array(
'cb' => $cb,
'username' => $un,
'display_name' => __( 'Name' )
), $old);
}
return $cols;
}
add_filter('manage_users_custom_column', 'show_diplay_name_as_name', 9999, 3);
function show_diplay_name_as_name ( $now, $column_name, $uid) {
if ( $column_name === 'display_name' ) {
$user = new WP_User($uid);
$now = $user->display_name;
}
return $now;
}