0

I have a custom built site that does not use usernames and everything requires use of their email address. The username is auto generated and looks bad in the table.

I know there are filters and actions to work with custom columns within the users table but I have been looking for a way to replace the username with the email address.

I know I can unset the username using manage_users_columns filter but I then loose the row actions.

Is there a way to accomplish this without rewriting wordpress core functions?

Edit - Clarification: I am looking to replace the username column in the users.php table.

2
  • WordPress doesn't auto-generate usernames by default & requires one to be entered manually - are you using a plugin for your user registration? And are you wanting to implement a filter for new registrations in the future, or are you also asking about bulk-updating existing users? Jun 2 at 7:51
  • I built a custom api for use with a mobile application that only requires email. My API generates a random string for their username since a recent wp update prevents the use of an email address.
    – James
    Jun 2 at 12:43

2 Answers 2

1

It seems that the username column content is hardcoded in single_row() of WP_Users_List_Table and there isn't a filter you can use to change it.

A hacky way is to enqueue some javascript on the users view and have the script change the content.

Or take the rocky road by duplicating the code that handles the row actions in single_row(), and replace the default column with a custom one showing the email and the actions.

Or just brute force the email as the username with wp_pre_insert_user_data filter.

0
function wpse406344_replace_usernames() {
    
    //Get all the users
    $users = get_users();
    
    //Loop through them and update user_login table
    foreach( $users as $user ) {
        wp_insert_user(array(
            'ID' => $user->ID,
            'user_login' => $user->user_email,
        ));
    }
    
}
add_action( 'init', 'wpse406344_replace_usernames' );

You need to run this only once. After successful execution, remove this. Also note, this will only update existing users. If you want to set username for future registrations, you can use the user_register hook.

1
  • Im not looking to set the email address as the username field. I want to remove the username column from the users.php page.
    – James
    Jun 2 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.