0

I'm creating my own routes for the wodpress api. At some point I need the rest content of the post and pages, to do this i have this function:

function get_rest_content($id, $type)
{
    if ($id > 0) {
        $request = new WP_REST_Request('GET', '/wp/v2/'.$type.'/' . $id);
        $response = rest_do_request($request)->data;
    } else {
        $response = null;
    }


    if (empty($response)) {
        return new WP_Error('wpse-error',
            esc_html__('No '.$type. 'found', 'wpse'),
            ['status' => 404]);
    }
    return $response;
}

$post_1 = get_rest_content(1,'posts') // give me the rest content of the post with id=1

but if I want to have the post content with embed data I change:

new WP_REST_Request('GET', '/wp/v2/'.$type.'/' . $id);

to

new WP_REST_Request('GET', '/wp/v2/'.$type.'/' . $id . '?_embed=true');

but this new request returns rest_no_route error

6
  • Your request with embed seems correct to me. On my installation "me.local.com/wp-json/wp/v2/posts/296?_embed=true" works. Try entering the equivalent for your type and id in your browser. What response do you get?
    – user8262086
    Sep 8, 2017 at 2:06
  • @user8262086, if I use the address manually on the browser this works as I spect but in this code no, maybe something fail with the WP_REST_Request I also try with WP_REST_Request::from_url as the documentation say and give me the same error
    – efirvida
    Sep 8, 2017 at 2:47
  • When get_rest_content is called have your rest routes already been registered?
    – user8262086
    Sep 8, 2017 at 3:07
  • @user8262086 I update my question with my routes and callbacks
    – efirvida
    Sep 8, 2017 at 3:46
  • As an experiment can you try "new WP_REST_Request('GET', '/wp/v2/posts/999?_embed=true');" where 999 is a valid post id on your system. I am trying to find out if the problem is specific to your route or your code so I want to run your code on a known good route.
    – user8262086
    Sep 8, 2017 at 3:56

2 Answers 2

2

I have read the source code and now understand. The second parameter of new WP_REST_Request() is the route only without query parameters. The query parameters are specified in another method. E.g.,

$request = new WP_REST_Request( 'GET', 'wp/v2/posts/999' );
$request->set_query_params( [ '_embed' => '1' ] );

However, this will not work as '_embed' is a special query parameter. It is not handled by WP_REST_Server::dispatch(), which means rest_do_request() will not handle '_embed' as rest_do_request() is just a wrapper of WP_REST_Server::dispatch().

The reason '_embed' works from a URL is that URLs are processed by WP_REST_Server::serve_request() which calls WP_REST_Server::dispatch() but also calls WP_REST_Server::response_to_data() which calls WP_REST_Server::embed_links().

If you want '_embed' to work in your get_rest_content() you will need to add the code for WP_REST_Server::embed_links().

0

I found a Github issue but the workaround is not working for me (at least for my code+WordPress version): https://github.com/WP-API/WP-API/issues/2857

Did you try adding the embeddable links to the response?

//get the post
$response = rest_do_request($request)->get_data();

//add the embeddable links 
$results_with_embed = rest_ensure_response(rest_get_server()->response_to_data( $response, true ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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