How to add custom fields to REST API response WordPress?

WordPress REST API has become a good way to interact with WordPress. It has very well support for REST APIs. All the rest endpoints are standard and highly customizable.

If we add custom fields to different WordPress resources, we need to write custom code to get those custom fields in the REST API response.

WordPress post endpoints:

When we want to use post endpoints in WordPress we may need all the custom meta fields of the post with REST API response.

Following are the two endpoints for user to get userlist and to get a single user.

# Get posts list 
/wp-json/wp/v2/posts

# Get a single post
/wp-json/wp/v2/posts/<id>

Get all meta fields for a post:

If we need the post meta fields with WordPress REST API response, we can add the following code to get all the meta fields in the response. Following is the code segment:

add_action( 'rest_api_init', 'create_api_posts_meta_field' );

function create_api_posts_meta_field() {
    // register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
    register_rest_field( 'post', 'postmeta', array(
            'get_callback'    => 'get_post_meta_for_api',
            'schema'          => null,
        )
    );
}

function get_post_meta_for_api( $obj ) {
    // return the post meta
    return get_post_meta( $obj['id'] );
}

WordPress user endpoints:

When we want to use user endpoints in WordPress we may need all the custom meta fields of the user with REST API response.

Following are the two endpoints for user to get userlist and to get a single user.

# Get user list 
/wp-json/wp/v2/users

# Get a single user
/wp-json/wp/v2/users/<id>

Get user’s all meta fields:

If we need the user meta fields with WordPress REST API response, we can add the following code to get all the meta fields in the response. Following is the code segment:

function get_user_meta_for_restapi($user, $field_name, $request) {
    return get_userdata( $user['id'] );
}

function create_restapi_user_obj_field() {
    // register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
    register_rest_field('user', 'usermeta', array(
        'get_callback'    => 'get_user_meta_for_restapi',
        'schema'          => null,
    ));

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

Get first and last names REST API response:

The WordPress standard user REST API might not return the first_name and last_name with API response. We can add it with the REST API response easily. Following is the code segment:

function get_user_meta_for_restapi($user, $field_name, $request) {
    $userObj = get_userdata( $user['id'] );

    return array (
        'firstname' => $userObj->first_name,
        'lastname' => $userObj->last_name,
        'email' => $userObj->user_email,
    );
}

function create_restapi_user_obj_field() {
    // register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
    register_rest_field('user', 'usermeta', array(
        'get_callback'    => 'get_user_meta_for_restapi',
        'schema'          => null,
    ));

}
add_action('rest_api_init', 'create_restapi_user_obj_field');
Reference: