1

I'm trying to get the First Name and the Last Name of the user in the wp-json/wp/v2/users/1 response, all those details are missing since I switched to v2.

I have tried:

function mm_wpapiv2_user_first_name($object, $field_name, $request) {
    return "Test";
}

register_rest_field( 'user', 'first_name',
   array('get_callback'    => 'mm_wpapiv2_user_first_name')
);

but the field isn't being added to the response. Please help!

2

1 Answer 1

2

Solution A - Prepare User Response

You can filter the response for a user to include any property you want with rest_prepare_user.

add_filter( 'rest_prepare_user', function( $response, $user, $request ) {

    $response->data[ 'first_name' ] = get_user_meta( $user->ID, 'first_name', true );
    $response->data[ 'last_name' ] = get_user_meta( $user->ID, 'last_name', true );

    return $response;

}, 10, 3 );

Solution B - Update Schema

Another way is to not only use register the rest field but update the context to be publicly visible for existing hidden items.

/**
 * Return field data for User
 *
 * @param array           $object
 * @param string          $field_name
 * @param WP_REST_Request $request
 *
 * @return string
 */
function get_rest_api_field_data( $object, $field_name, $request ) {

    switch ( $field_name ) {
        case 'first_name' :
            return get_user_meta( $object[ 'id' ], 'first_name', true );
        case 'last_name' :
            return get_user_meta( $object[ 'id' ], 'last_name', true );
    }
}

/**
 * Register fields for User
 */
function add_custom_rest_fields_for_users() {

    // register the First Name of the User -- Visible to anyone

    register_rest_field( 'user', 'first_name', array (
        'get_callback'    => 'get_rest_api_field_data',
        'update_callback' => null,
        'schema'          => array (
            'description' => __( 'First name for the resource.' ),
            'type'        => 'string',
            'context'     => array ( 'embed', 'view', 'edit' ), // Adding `embed` and `view`
            'arg_options' => array (
                'sanitize_callback' => 'sanitize_text_field',
            ),
        ),
    ) );

    // register the Last Name of the User -- Visible to anyone

    register_rest_field( 'user', 'last_name', array (
        'get_callback'    => 'get_rest_api_field_data',
        'update_callback' => null,
        'schema'          => array (
            'description' => __( 'Last name for the resource.' ),
            'type'        => 'string',
            'context'     => array ( 'embed', 'view', 'edit' ), // Adding `embed` and `view`
            'arg_options' => array (
                'sanitize_callback' => 'sanitize_text_field',
            ),
        ),

    ) );
}

add_action( 'rest_api_init', 'add_custom_rest_fields_for_users' );

Extra Info

The original response was constructed just before the filter had been called. As you see in prepare_item_for_response(), $schema['properties'] is checked before adding first_name or last_name.

if ( ! empty( $schema['properties']['first_name'] ) ) {
    $data['first_name'] = $user->first_name;
}
if ( ! empty( $schema['properties']['last_name'] ) ) {
    $data['last_name'] = $user->last_name;
}

Under get_item_schema () you can see how this schema has been created -- it's worth noting the missing 'embed', 'view' which is why these aren't rendered without authentication and under more unique conditions.

$schema = array(
    '$schema'    => 'http://json-schema.org/draft-04/schema#',
    'title'      => 'user',
    'type'       => 'object',
    'properties' => array(
        'id'          => array(
            'description' => __( 'Unique identifier for the resource.' ),
            'type'        => 'integer',
            'context'     => array( 'embed', 'view', 'edit' ),
            'readonly'    => true,
        ),
        'username'    => array(
            'description' => __( 'Login name for the resource.' ),
            'type'        => 'string',
            'context'     => array( 'edit' ),
            'required'    => true,
            'arg_options' => array(
                'sanitize_callback' => 'sanitize_user',
            ),
        ),
        'name'        => array(
            'description' => __( 'Display name for the resource.' ),
            'type'        => 'string',
            'context'     => array( 'embed', 'view', 'edit' ),
            'arg_options' => array(
                'sanitize_callback' => 'sanitize_text_field',
            ),
        ),
        'first_name'  => array(
            'description' => __( 'First name for the resource.' ),
            'type'        => 'string',
            'context'     => array( 'edit' ),
            'arg_options' => array(
                'sanitize_callback' => 'sanitize_text_field',
            ),
        ),
        'last_name'   => array(
            'description' => __( 'Last name for the resource.' ),
            'type'        => 'string',
            'context'     => array( 'edit' ),
            'arg_options' => array(
                'sanitize_callback' => 'sanitize_text_field',
            ),
        ),

Reference

2
  • "name" is actually the username and not first name/ last name. Both of those are missing from the response. "Human Made" isn't the best example to be used for a username...
    – pr0bl3m
    Commented Nov 3, 2016 at 15:13
  • Yup, you're correct ;)
    – jgraup
    Commented Nov 3, 2016 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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