Home ยป Change WP REST API response

Change WP REST API response

This helps to alter Rest API response.

Use unset($data->data['avatar_urls']); to unset a key.

Use $data->remove_link('collection'); to remove a link.

Use $data->data['new_value'] = "this can be anything"; to add new value.

function unset_json($data, $post, $context) {
// unset key
    unset($data->data['avatar_urls']);
    unset($data->data['link']);
    unset($data->data['_links']);
    unset($data->data['url']);
    unset($data->data['meta']);
// remove link
    $data->remove_link('collection');
    $data->remove_link('self');
    $data->remove_link('about');
    $data->remove_link('author');
    $data->remove_link('replies');
    $data->remove_link('version-history');
    $data->remove_link('predecessor-version');
    $data->remove_link('up');
    $data->remove_link('https://api.w.org/featuredmedia');
    $data->remove_link('https://api.w.org/attachment');
    $data->remove_link('https://api.w.org/term');
    $data->remove_link('https://api.w.org/post_type');
    $data->remove_link('curies');


// add new value
 $data->data['new_value'] = "this can be anything";

    return $data;
}

add_filter('rest_prepare_user', 'unset_json', 10, 3); // for user
add_filter('rest_prepare_post', 'unset_json', 10, 3); // for post

Leave a Reply