Joomla & WordPress Tutorials, Info, Discussion, Tips | GavickPro Blog

Adding custom fields in the WordPress REST API

When we look at the standard REST API responses it’s clear to see that many download data operations will need additional queries to be effective. One possible solution for this issue is the addition of custom fields to the responses the REST API returns.

What’s the best way to add custom fields in the WordPress?

There’s two possible methods for adding custom fields to to REST API responses:

The first method is technically easier to implement, so I would recommend it when we have to modify REST API responses. However, before we can use the first method we need to add support for the rest_insert_post action so that we can add posts that include the our new field.

Additionally, there are some important principles to bear in mind when working with this method:

Adding a new field with the rest_prepare_post filter

if(!function_exists('slug_add_data')) :
    function slug_add_data($response, $post) {
        $response->data['slug_category'] = '';
        $categories = get_the_category($post->ID);

        if(count($categories)) {
            $category_slug = $categories[0]->slug;
            $response->data['slug_category'] = $category_slug;
         }
    }
}

add_filter('rest_prepare_post', 'slug_add_data', 10, 3);

In the above example we add a slug_category field to the /wp/v2/posts endpoint in the REST API response, which will include the simplified name of the first category to which the post was added.

Adding a new field with the register_api_field function

The described below requires more code, but it’s more elegant and opens up many more possibilities for expanding our functionality.

The code adding meta data to results containing posts will look something like this:

function slug_add_post_data() {
    register_api_field('post', 
        'slug_field', 
        array(
            'get_callback' => 'slug_get_field',
            'update_callback' => 'slug_update_field',
            'schema' => array(
                                'description' => 'My special field',
                                'type' => 'string',
                                'context' => array('view', 'edit')
                            )
        )
    );
}

add_action('rest_api_init', 'slug_add_post_data');

function slug_get_field($post, $field_name, $request) {
  return get_post_meta($post->id, $field_name);
}

function slug_update_field($value, $post, $field_name) {
  if (!$value || !is_string($value)) {
    return;
  }

  return update_post_meta($post->ID, $field_name, strip_tags($value));
}

The process of adding a field is achieved via the rest_api_init action by calling the register_api_field function. This function takes three arguments:

The most important is the last argument, as here we specify:

The function responsible for returning the data has access to the post object, the field name, and query object. It will be used when using GET queries.

On the other hand, the update function has access to a new value, post object and the field name. It will be used with POST and PUT/PATCH operations.

The last element of the array is a schema element; specifying its value is not required, but if we want our field to be described in the structure of its endpoint, then it’s worth adding a short associative array that includes information about this field (as seen in the above code).

Summary

The standard REST API may not offer an optimal solution to get all the data we need, but using its features, filters and actions we may easily add missing fields. In my next article discussing the REST API, we’ll look at creating our own custom endpo

Share