且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Wordpress 更改默认显示名称 Publicy 对于所有现有用户

更新时间:2023-01-30 09:30:03

使用 admin_head 钩子的问题是它对不使用管理系统的用户不起作用.此外,我尝试实施 Marty 发布的解决方案失败了,因为似乎无法通过 update_user_meta() 更新 display_name - 您必须使用 wp_update_user().

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().

我的建议 - 把它放在你的functions.php文件中:

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); 

对我来说(使用 WP 3.4.1),这工作正常,在他们登录后立即替换显示名称.

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