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