I want to modify the REST-API (media-endpoint) and add a new field called media_category
.
First I tried this with the post-endpoint, this has worked well, but when I try the same with the media-endpoint it does not work.
Is the object type for the media-endpoint anything else than media?
My current code:
<?php
/**
* Plugin Name: REST Response Modifier
* Description: A simple plugin to modify the rest api
* Author: TheBalco
* Author URI: http://somepage.dev
*/
add_action('rest_api_init', 'tb_add_custom_rest_fields');
function tb_add_custom_rest_fields() {
// schema
$media_category_schema = array(
'description' => 'Categories of the media item',
'type' => 'string',
'context' => ['view']
);
// registering the field
register_rest_field(
'media',
'media_category',
[
'get_callback' => 'get_media_category',
'update_callback' => null,
'schema' => $media_category_schema
]
);
}
/**
* Callback
* @param array $object The current post object
* @param string $field_name The name of the field
* @param WP_REST_request $request The current request
* @return string The return value
*/
function get_media_category($object, $field_name, $request) {
return 'this-is-a-test';
//return get_the_author_meta( 'display_name', $object['author'] );
}
If I replace the media
in register_rest_field
with post
, it works for the post-endpoint. But it doesn't work for the media-endpoint.
Anybody got a solution for this?