0

The question may seems dumb but i need to access wordpress's default rest api from my custom wordpress plugin so that i can modify further for my custom api endpoints and post data to a third party api.

I need to access and have all the data and functionalities of the existing rest api but i don't want to fetch data from database by querying again. Instead i want to call the rest api directly from plugin.

Now the problem it doesn't seems right to calling rest api from a plugin. If i want to avoid querying again from database is this a valid approach (calling rest api) or there are other better ways to do that?

2 Answers 2

0

According to the Wordpress Rest API HandBook, To modify Responses, You can use register_rest_field and register_meta.

More Details : Modifying Responses


To Create an Endpoint you can use , register_rest_route().

More Details : Routes and Endpoints

Sign up to request clarification or add additional context in comments.

Comments

0

It isn’t difficult to make an internal REST API call, but it is a bit more involved than your normal WP_Query usage. Also, keep in mind that the parameters you pass to the REST API are not always the same as what you would pass to WP_Query. For example, the post_status query var would become status when making an API request.

<?php
  $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
  $request->set_query_params( [ 'per_page' => 12 ] );
  $response = rest_do_request( $request );
  $server = rest_get_server();
  $data = $server->response_to_data( 
  $response, false );
  $json = wp_json_encode( $data );

You can read more about in this URL

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.