我想修改REST-API(媒体端点)并添加一个名为media_category
.首先,我在post端点上尝试了这种方法,效果很好,但在媒体端点上尝试同样的方法时,效果并不理想。
媒体端点的对象类型是否为媒体以外的其他类型?
我的当前代码:
<?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\'] );
}
如果我更换media
在里面register_rest_field
具有post
, 它适用于post端点。但它不适用于媒体端点。有人有办法吗?