WP REST API: Modifying the JSON Response

Originally Written By: Boris Kuzmanov (preserved in its originality.)

The WordPress API includes several endpoints you can use to read information from the database and use the data for different purposes. In this blog post we will get use of the /wp-json/wp/v2/posts endpoint, which is something like running a WP_Query. It works using the four most common HTTP methods – GET, POST, PUT and DELETE, for which you will need to be authorized to work with.

* Note that you will need to install the WP REST API plugin.

Because we mostly don’t need and use all the information, there is a way to manipulate the JSON response. WP API provides us with a filter called rest_prepare_posts which we can use for adding and removing fields (data). So, lets jump into some examples.

Adding fields
Because not all of the post’s data that we may need is included in the JSON response, for example the custom meta fields or URL of the featured image (the ID of the featured image is included), we can easily add them using the rest_prepare_posts filter.

Including Custom Meta Field
function filter_post_json( $data, $post, $context ) {
$source = get_post_meta( $post->ID, '_source', true ); // get the value from the meta field
if( $source ) { // include it in the response if not empty
$data->data['custom_meta'] = array( 'source' => $source );
}

return $data;
}
add_filter( 'rest_prepare_post', 'filter_post_json', 10, 3 );

Including Featured Image URL
function post_fetured_image_json( $data, $post, $context ) {
$featured_image_id = $data->data['featured_media']; // get featured image id
$featured_image_url = wp_get_attachment_image_src( $featured_image_id, 'original' ); // get url of the original size
if( $featured_image_url ) {
$data->data['featured_image_url'] = $featured_image_url[0];
}

return $data;
}
add_filter( 'rest_prepare_post', 'post_fetured_image_json', 10, 3 );

Removing fields
Since we have the featured image URL and we don’t need the featured image ID, there is no need to have it in the JSON response. We can easily remove it using the PHP unset function (line 10).
function post_fetured_image_json( $data, $post, $context ) {
$featured_image_id = $data->data['featured_media']; // get featured image id
$featured_image_url = wp_get_attachment_image_src( $featured_image_id, 'original' ); // get url of the original size
if( $featured_image_id ) {
$data->data['featured_image_url'] = $featured_image_url[0];
}
unset( $data->data['featured_media'] ); // remove the featured_media field

return $data;
}
add_filter( 'rest_prepare_post', 'post_fetured_image_json', 10, 3 );

Modifying the response
Now, since we’ve learned how to add and remove fields from the JSON response, one small example for the end will be how to include only specific fields (data) in the response, without doing unset and other stuff. Lets assume that we will need only the id, title and the link of the post. To avoid doing unset on every other field, we can get them this way:
function get_all_posts( $data, $post, $context ) {
return [
'id' => $data->data['id'],
'title' => $data->data['title']['rendered'],
'link' => $data->data['link'],
];
}
add_filter( 'rest_prepare_post', 'get_all_posts', 10, 3 );

If you want to read more about modifying the response, there is a full explanation in the documentation.