今天缙哥哥想增加个人信息字段,方便管理中医体质档案,可是发现默认情况下,WordPress 后台让用户可以在后台设置:姓,名,昵称,然后选择显示的名称。大概就是下图这个样子:

简直神烦有木有?搞的跟个外国佬一样,中国哪那么复杂,顶多也就姓名、昵称、显示三项。后面一想,一般人总不会显示姓名吧,那显示的选项也就没有必要了,为什么不直接留个昵称就好了呢?
其实只是用来写写博客,很少的编辑会填这么多的东西,但是如果删掉的话,又怕某些字段需要引用,所以最好的方法就是把他们隐藏起来,看了一下 WordPress 源代码,名称设置这里竟然没有 filter,没有filter 那就用 JS 来隐藏,然后提交的时候,把显示的名称强制设置为昵称就好了。
最后的代码如下,同样复制到当前主题的 functions.php 文件即可:
/**
* 四合一简化 WordPress 后台用户个人信息姓名昵称设置
* https://www.dujin.org/fenxiang/wp/10138.html
*/
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'] = '';
} |
/**
* 四合一简化 WordPress 后台用户个人信息姓名昵称设置
* https://www.dujin.org/fenxiang/wp/10138.html
*/
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://www.wpdaxue.com/wordpress-custom-user-metas.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32364.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57