Wordpress - REST API, get user role?

This is totally possible by registering your own rest field into the response.

Here's some documentation on modifying response data. https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/

Here's how to add roles to the endpoint:

function get_user_roles($object, $field_name, $request) {
  return get_userdata($object['id'])->roles;
}

add_action('rest_api_init', function() {
  register_rest_field('user', 'roles', array(
    'get_callback' => 'get_user_roles',
    'update_callback' => null,
    'schema' => array(
      'type' => 'array'
    )
  ));
});

Specifically, what I'm doing is creating a function called get_user_roles that retrieves the user roles, then registering a 'roles' field in the response, and tell it that the data comes from that function.

I also ran into an issue of it not playing nice when the data returned an array, so I told the schema to expect it in the schema property of the array passed in register_rest_field


You just need to pass the context parameter with a value of edit in your url:

http://demo.wp-api.org/wp-json/wp/v2/users/1?context=edit

According to the WordPress REST API Handbook, you have to pass the context=edit to make this attribute appear in the response. If you do not include ?context=edit the roles attribute will not be shown.