Extend the WordPress REST API with register_rest_field()

Extend the WordPress REST API with register_rest_field()

The REST API returns a fixed set of fields for every post: id, title, content, status and so on. Data that a theme or plugin keeps in post meta, in terms or in its own tables is not there. With register_rest_field() you add it to the existing response, without building a separate endpoint.

Problem: A JavaScript front end or a mobile app needs a custom field of the post, and the standard /wp/v2/posts response does not contain it.

Solution: Register the field on rest_api_init with a get_callback that returns the value, an optional update_callback that saves it, and a schema. For plain post meta, register_post_meta() with show_in_rest is often enough.

A readable and writable field

<?php
add_action( 'rest_api_init', 'ha_register_reading_time_field' );

function ha_register_reading_time_field() {
    register_rest_field( 'post', 'reading_time', array(
        'get_callback'    => function ( $post ) {
            return (int) get_post_meta( $post['id'], '_reading_time', true );
        },
        'update_callback' => function ( $value, $post ) {
            return update_post_meta( $post->ID, '_reading_time', (int) $value );
        },
        'schema'          => array(
            'description' => 'Estimated reading time in minutes.',
            'type'        => 'integer',
            'context'     => array( 'view', 'edit' ),
        ),
    ) );
}

GET /wp-json/wp/v2/posts/123 now contains "reading_time": 5. A POST to the same URL with {"reading_time": 8} by an authenticated user who can edit the post saves the value: the permission check of the posts endpoint applies to your field as well. The get_callback gets the response data as an array, the update_callback gets the WP_Post object.

A field for a custom post type

The post type must be registered with 'show_in_rest' => true:

<?php
add_action( 'rest_api_init', function () {
    register_rest_field( 'book', 'isbn', array(
        'get_callback' => function ( $post ) {
            return get_post_meta( $post['id'], 'isbn', true );
        },
        'schema'       => array( 'type' => 'string', 'context' => array( 'view' ) ),
    ) );
} );

Term names as a simple list

The response contains term IDs only. For a front end that just prints the tags, a list of names saves a second request:

<?php
add_action( 'rest_api_init', function () {
    register_rest_field( 'post', 'tag_names', array(
        'get_callback' => function ( $post ) {
            $terms = get_the_terms( $post['id'], 'post_tag' );
            return ( $terms && ! is_wp_error( $terms ) ) ? wp_list_pluck( $terms, 'name' ) : array();
        },
        'schema'       => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ),
    ) );
} );

How to read posts from the API in a separate front end is shown in using the REST API as a headless CMS backend.

Or simply register the meta

If the value is one meta field without any calculation, register the meta instead; it then appears inside the meta object of the response:

<?php
register_post_meta( 'post', 'subtitle', array(
    'type'         => 'string',
    'single'       => true,
    'show_in_rest' => true,
) );

NOTE: The get_callback runs for every post of every response, also in lists of 100 posts. Keep it cheap: no remote requests and no heavy queries per post. And a field in the view context is public for public posts, so never return private data (emails, internal notes) without a current_user_can() check in the callback.