remove _links object from wp rest api response by filter or hook
Asked Answered
C

5

6

I have removed unwanted data by unset($data->data['field_name']) from json output. For this I am using wordpress filter rest_prepare_.

But how we remove _links object from JSON output?

Cheese answered 11/7, 2017 at 7:14 Comment(0)
S
5

I don’t know how to unset, but you can set which variables to return.

function prepare_rest($data, $post, $request){
    return [
        'id'    => $data->data['id'],
        'title' => $data->data['title']['rendered']
    ];
}

add_filter('rest_prepare_post', 'prepare_rest', 10, 3);
Sibella answered 3/10, 2017 at 19:12 Comment(1)
Unfortunatelly, this way not allow adding protected items, like _embedded.Lithosphere
S
5

Gotta be careful removing stuff from the API, but if you're sure you want to remove this, it can be done like this:

function so_45027789_rest_prepare_post($data, $post, $request)
{
    foreach($data->get_links() as $_linkKey => $_linkVal) {
        $data->remove_link($_linkKey);
    }
    return $data;
}

add_filter('rest_prepare_post', 'so_45027789_rest_prepare_post', 1, 3);

I'd recommend only doing so if you're explicitly asking for a smaller response in your implementation with the API, thereby leaving default usage of the API untouched, like via a query variable:

function so_45027789_rest_prepare_post($data, $post, $request)
{
    $params = $request->get_params();
    if(isset($params['_minimal'])) {
        foreach($data->get_links() as $_linkKey => $_linkVal) {
            $data->remove_link($_linkKey);
        }
    }
    return $data;
}

add_filter('rest_prepare_post', 'so_45027789_rest_prepare_post', 1, 3);
Shindig answered 27/11, 2018 at 17:54 Comment(0)
L
4

This is not very good solution but work.

function contract_remove_links( $data, $post, $context ) {

    $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( 'https://api.w.org/featuredmedia' );
    $data->remove_link( 'https://api.w.org/attachment' );
    $data->remove_link( 'https://api.w.org/term' );
    $data->remove_link( 'curies' );

    return $data;
}

add_filter( 'rest_prepare_post', 'contract_remove_links', 10, 3 );
Lubricity answered 26/3, 2018 at 10:52 Comment(0)
V
0

The rest_prepare_post is the right filter to use, but the _links field is dynamically generated by WordPress. So you can’t remove things to it directly.

you can add your own link in this parameter or you can remove the default link from the "_links".

Here is the code....

add_filter( 'rest_prepare_post', function ( $response ) {
$response->add_link( 'yourrel', rest_url( 'yourendpoint/thing' ), array(
    'embeddable' => true,
) );

$response->remove_link( 'collection' );

return $response;
} );
Valence answered 15/3, 2018 at 6:54 Comment(1)
How do you specify to replace the link in routes ->_links -> self -> href as well as the root url and home with another link?Emend
P
-1

Unfortunately, you cannot remove it, it’s protected.

    unset( $data->links );

    PHP Fatal error:  Uncaught Error: Cannot access protected property WP_REST_Response::$links 

    /wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(311)
        Line 1567:  $response->add_links( $this->prepare_links( $post ) );
        Line 1608:  protected function prepare_links( $post ) {
Peppi answered 10/10, 2017 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.