Hide username from users list

There are no direct filters using which we can change the content of Name column. So to get what you want, we should remove ‘Name’ column too and create our own Name column. Therefore, modify_user_columns function will be like this

function modify_user_columns($column) {
    $column = array(
        "cb" => "<input type=\"checkbox\" />",
        "wdm_name" => __('Name'),
        "email" => "E-mail",
        "birthdate" => "Narozeniny",
        "sleva_moto" => "Sleva moto"


    );
    return $column;
}

Now, we have a control on what we can show in the Name column. We can add content in that Name column using filter manage_users_custom_column.

add_filter( 'manage_users_custom_column', 'wdm_display_name_with_edit_link', 10, 3 );

function wdm_display_name_with_edit_link($content, $column_name, $user_object_id){
    if ($column_name == 'wdm_name' && 
        current_user_can( 'edit_user',  $user_object_id) ) {

        //get info of user
        $user_object = get_userdata($user_object_id); 

        //generate edit link
        $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user_object->ID ) ) ); 

        //Generate the complete Name with edit link
        $content = "<strong><a href=\"$edit_link\">$user_object->first_name $user_object->last_name</a></strong><br/>"; 
    } else {
        //If logged in user does not have rights 
        //to edit users, just show Name of users
        $content = "<strong>$user_object->first_name $user_object->last_name</strong><br />"; 
    }

    return $content;
}

Now it will show Names with Edit link. Hope this helps. 🙂

Leave a Comment