Wordpress Change Default Display Name Publicy As for all existing users

前端 未结 6 658
耶瑟儿~
耶瑟儿~ 2021-02-05 23:58

does anyone know of way I can change the existing display name publicly as for all users. I want to have firstname lastname as the default because this will reflect in the forum

6条回答
  •  無奈伤痛
    2021-02-06 00:30

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

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

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

提交回复
热议问题