如何强制显示名称与用户名相同?

时间:2018-08-11 作者:csandreas1

我需要帮助。我的用户注册到网站。他们键入用户名user_login (请参见chello),但显示名称(请参见chello04)不同。它的末尾有两个随机数字。

有没有办法强制显示名称与用户名相同?

enter image description here

enter image description here

2 个回复
最合适的回答,由SO网友:Nathan Johnson 整理而成

You can use the wp_pre_insert_user_data filter.

function wpse_filter_user_data( $data, $update, $id) {
  if( isset( $data[ \'user_login\' ] ) ) {
    $data[ \'display_name\' ] = $data[ \'user_login\' ];
    return $data;
  }
  $user = get_user_by( \'email\', $data[ \'user_email\' ] );
  $data[ \'display_name\' ] = $user->user_login;
  return $data;
}
add_filter( \'wp_pre_insert_user_data\', \'wpse_filter_user_data\', 10, 3 );

You\'ll probably want to use Javascript and/or CSS to hide the field too for a better user experience.

$( \'.user-display-name-wrap\' ).remove();

.user-display-name-wrap {
  display:none;
}
SO网友:Andrea Somovigo

我会使用jQuery:

add_action(\'admin_footer\',\'myPlugin_fixed_user_name\');

function myPlugin_fixed_user_name(){
  ob_start();
  ?>
  <script>
  jQuery(document).ready(function($){
    if( pagenow == \'profile\' || pagenow == \'user-edit\' ){// up to you decide wether to run on edit user ( made by admin ) also or only on personal profile 
      var uName=$(\'#user_login\').val();
      $(\'#display_name option[text="\'+uName+\'"]\').attr(\'selected\',\'selected\');
      $(\'#display_name\').attr(\'disabled\',true);
    }
  })
  </script>
  <?php
  echo ob_get_clean();
  }

//To be 100% safe you can also run a function on user profile update:

add_action(\'personal_options_update\', \'my_update_extra_profile_fields\');

function my_update_extra_profile_fields($user_id) {
  if ( current_user_can(\'edit_user\',$user_id) )
     wp_update_user( array( \'ID\' => $user_id, \'display_name\' => $_POST[\'user_login\'] ) );
}

结束

相关推荐

更改wp-admin/plugins.php上统计的插件数量

我已成功地使用从插件页面隐藏我的插件$wp_list_table 然而,顶部的分页仍然将插件列为“所有(3)”等。我成功地改变了$wp_list_table 的数组_pagination_args = total_items.但它仍然在页面顶部呈现插件-“全部(3)”。有什么办法可以解决这个问题吗?我找到了WP_Plugins_List_Table::prepare_items()具有全局$totals 变量,但我不确定我将如何改变这一点,在这个函数中$totals = array(); fore