WP REST API V2-将用户数据添加到响应

时间:2016-11-03 作者:adriand

我正在尝试获取wp-json/wp/v2/users/1 回复,自从我切换到v2后,所有这些细节都丢失了。

我尝试过:

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\')
);
但该字段没有添加到响应中。请帮忙!

1 个回复
SO网友:jgraup

解决方案A-准备用户响应user 要包含您想要的任何属性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 );
解决方案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_namelast_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-896
  • https://code.tutsplus.com/tutorials/wp-rest-api-internals-and-customization--cms-24945
  • http://v2.wp-api.org/extending/modifying/#how-to-use-registerrestfield
  • 相关推荐

    如何使用wp-api向用户添加元字段?

    如何使用wp api添加、更新和检索用户元字段?我添加了一个函数来向用户添加元字段phonenumber。这样的函数是否需要向元对象添加元值?该函数不会将字段添加到API响应中的元对象,而是将其作为新字段添加。我现在拥有的功能:<?php function portal_add_user_field() { register_rest_field( \'user\', \'phonenumber\', array( \'