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 );
解决方案B-更新架构另一种方法是
register the rest field 但要更新上下文,使其对现有隐藏项公开可见。
/**
* 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\' );
原始响应是在调用筛选器之前构建的。如你所见
prepare_item_for_response()
,
$schema[\'properties\']
添加前已选中
first_name
或
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;
}
低于
get_item_schema ()
你可以看到
schema 已创建--值得注意的是缺少
\'embed\', \'view\'
这就是为什么没有
authentication 在更独特的
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\',
),
),
参考
https://code.tutsplus.com/series/introducing-the-wp-rest-api--cms-896https://code.tutsplus.com/tutorials/wp-rest-api-internals-and-customization--cms-24945http://v2.wp-api.org/extending/modifying/#how-to-use-registerrestfield