默認情況下,WordPress 讓用戶可以在后臺設置:姓,名,昵稱,然后選擇顯示的名稱。大概就是下圖這個樣子:

其實只是用來寫寫博客,很少的編輯會填這么多的東西,所以最好的方法就是把他們隱藏起來,看了一下 WordPress 源代碼,名稱設置這里竟然沒有 filter,沒有filter 那就用 JS 來隱藏,然后提交的時候,把顯示的名稱強制設置為昵稱就好了。
最后的代碼如下,同樣復制到當前主題的 functions.php 文件即可:
// 隱藏 姓,名 和 顯示的名稱,三個字段
add_action('show_user_profile','wpjam_edit_user_profile');
add_action('edit_user_profile','wpjam_edit_user_profile');
function wpjam_edit_user_profile($user){
?>
<script>
jQuery(document).ready(function($) {
$('#first_name').parent().parent().hide();
$('#last_name').parent().parent().hide();
$('#display_name').parent().parent().hide();
$('.show-admin-bar').hide();
});
</script>
<?php
}
//更新時候,強制設置顯示名稱為昵稱
add_action('personal_options_update','wpjam_edit_user_profile_update');
add_action('edit_user_profile_update','wpjam_edit_user_profile_update');
function wpjam_edit_user_profile_update($user_id){
if (!current_user_can('edit_user', $user_id))
return false;
$user = get_userdata($user_id);
$_POST['nickname'] = ($_POST['nickname'])?:$user->user_login;
$_POST['display_name'] = $_POST['nickname'];
$_POST['first_name'] = '';
$_POST['last_name'] = '';
}
最后的效果如下圖,是不是清爽多了:

來源于: https://blog.wpjam.com/m/simplify-wordpress-admin-user-name-fields/