2

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?

1 Answer 1

3

I found the solution for my problem. The object type for media is not media, it's just attachment. So the following code works for me:

<?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(
        'attachment',
        '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'] );
}
1
  • 1
    I can't yet, I have to wait 2 days :)
    – TheBalco
    Commented Mar 30, 2017 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.