i need help. My users register to the website. They type their username user_login (see chello) but the Display name (see chello04) is not the same. It has a random two digits at the end.
Is there a way to force Display names to be the same as usernames?
i need help. My users register to the website. They type their username user_login (see chello) but the Display name (see chello04) is not the same. It has a random two digits at the end.
Is there a way to force Display names to be the same as usernames?
Share Improve this question asked Aug 11, 2018 at 8:28 csandreas1csandreas1 2063 silver badges11 bronze badges2 Answers
Reset to default 4You can use the wp_pre_insert_user_data
filter.
function wpse_filter_user_data( $data, $update, $id) {
if( isset( $data[ 'user_login' ] ) ) {
$data[ 'display_name' ] = $data[ 'user_login' ];
return $data;
}
$user = get_user_by( 'email', $data[ 'user_email' ] );
$data[ 'display_name' ] = $user->user_login;
return $data;
}
add_filter( 'wp_pre_insert_user_data', 'wpse_filter_user_data', 10, 3 );
You'll probably want to use Javascript and/or CSS to hide the field too for a better user experience.
$( '.user-display-name-wrap' ).remove();
.user-display-name-wrap {
display:none;
}
I would do with jQuery:
add_action('admin_footer','myPlugin_fixed_user_name');
function myPlugin_fixed_user_name(){
ob_start();
?>
<script>
jQuery(document).ready(function($){
if( pagenow == 'profile' || pagenow == 'user-edit' ){// up to you decide wether to run on edit user ( made by admin ) also or only on personal profile
var uName=$('#user_login').val();
$('#display_name option[text="'+uName+'"]').attr('selected','selected');
$('#display_name').attr('disabled',true);
}
})
</script>
<?php
echo ob_get_clean();
}
//To be 100% safe you can also run a function on user profile update:
add_action('personal_options_update', 'my_update_extra_profile_fields');
function my_update_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) )
wp_update_user( array( 'ID' => $user_id, 'display_name' => $_POST['user_login'] ) );
}