Q:

How to get author meta into post endpoint in api v2

I'm trying to output all of the fields in the json result but seem to be using the object incorrectly. I want to add author meta fields to the post endpoint.
This doesn't work
    function slug_show_author_meta( $object, $field_name, $request ) {
      return  get_the_author_meta( $object[ 'id' ], $field_name );
    }

This does but only gives me one field
    function slug_show_author_meta( ) {
      return  get_the_author_meta('user_nicename');
    }

Ideally I'd just like to get user_nicenaem, user_email, display_name, nicename, first_name and last_name. Thanks!

A:

This should work:

function wpse_20160421_get_author_meta($object, $field_name, $request) {

    $user_data = get_userdata($object['author']); // get user data from author ID.

    $array_data = (array)($user_data->data); // object to array conversion.

    $array_data['first_name'] = get_user_meta($object['author'], 'first_name', true);
    $array_data['last_name']  = get_user_meta($object['author'], 'last_name', true);

    // prevent user enumeration.
    unset($array_data['user_login']);
    unset($array_data['user_pass']);
    unset($array_data['user_activation_key']);

    return array_filter($array_data);

}

function wpse_20160421_register_author_meta_rest_field() {

    register_rest_field('post', 'author_meta', array(
        'get_callback'    => 'wpse_20160421_get_author_meta',
        'update_callback' => null,
        'schema'          => null,
    ));

}
add_action('rest_api_init', 'wpse_20160421_register_author_meta_rest_field');

