Advertisement
Aurangajeb

Make WP First name & last name as default display name

Apr 11th, 2020
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. /** With WPUF it's better to set the profile update functions priority high **/
  2. /** Alternative solution as this plugin - https://wordpress.org/plugins/force-first-last/ **/
  3.  
  4. /* First name & last name as default display name. */
  5. add_action( 'profile_update', 'set_display_name', 20 );
  6.  
  7. function set_display_name( $user_id ) {
  8.  
  9.     $data = get_userdata( $user_id );
  10.  
  11.     if($data->first_name) {
  12.  
  13.         remove_action( 'profile_update', 'set_display_name', 10 ); // profile_update is called by wp_update_user, so we need to remove it before call, to avoid infinite recursion
  14.         wp_update_user(
  15.             array (
  16.                 'ID' => $user_id,
  17.                 'display_name' => "$data->first_name $data->last_name"
  18.             )
  19.         );
  20.         add_action( 'profile_update', 'set_display_name', 20 ); // set the priority high to override other plugins
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement