Igor Simic
4 years ago

Wordpress API custom route - access post parameters




In order to access the data of the POST request there is couple worpdress function that might help you. So let's assume that you already have set up custom POST route and now you want to access posted data in your callback function.It can be accessed by using some of these WP functions:

<?php
function my_awesome_func( WP_REST_Request $request ) {
  // You can access parameters via direct array access on the object:
 $param = $request['some_param'];
 
  // Or via the helper method:
 $param = $request->get_param( 'some_param' );
 
  // You can get the combined, merged set of parameters:
 $parameters = $request->get_params();
 
  // The individual sets of parameters are also available, if needed:
 $parameters = $request->get_url_params();
  $parameters = $request->get_query_params();
  $parameters = $request->get_body_params();
  $parameters = $request->get_json_params();
  $parameters = $request->get_default_params();
 
  // Uploads aren't merged in, but can be accessed separately:
 $parameters = $request->get_file_params();
}