I'm implementing a callback for a GET Wp Rest request, which receives a WP_Rest_Request
object.
function create_on_demand_post( \WP_REST_Request $request ) {
$params = $request->get_json_params();
// etc...
}
There are class methods which retrieve the request data, but I'm wondering if there's a way to access the data in this ArrayAccess implementation as I would with an array or object:
WP_REST_Request
method:"POST"
params:array(6)
headers:array(8)
body:"{"title": "From Python", "status": "publish", "content": "This is a post created using rest API", etc...}"
route:"/my-plugin/v1/posts"
attributes:array(7)
parsed_json:true
parsed_body:false
I have tried $request->body
, which doesn't evaluate and $request['body']
, as well as $request['body'][0]
, both of which return null
.
Are you only able to access the attributes using the provided methods?
I'm implementing a callback for a GET Wp Rest request, which receives a WP_Rest_Request
object.
function create_on_demand_post( \WP_REST_Request $request ) {
$params = $request->get_json_params();
// etc...
}
There are class methods which retrieve the request data, but I'm wondering if there's a way to access the data in this ArrayAccess implementation as I would with an array or object:
WP_REST_Request
method:"POST"
params:array(6)
headers:array(8)
body:"{"title": "From Python", "status": "publish", "content": "This is a post created using rest API", etc...}"
route:"/my-plugin/v1/posts"
attributes:array(7)
parsed_json:true
parsed_body:false
I have tried $request->body
, which doesn't evaluate and $request['body']
, as well as $request['body'][0]
, both of which return null
.
Are you only able to access the attributes using the provided methods?
Share Improve this question asked Jan 27, 2022 at 3:24 MikeiLLMikeiLL 5791 gold badge8 silver badges21 bronze badges 1 |1 Answer
Reset to default 0As noted above in Sally CJ's comment, and as seen in the Wp Codebase, those are protected properties, so they are meant to be accessed only through the multiple get
and set
methods that the class exposes:
/**
* Sets the header on request.
*
* @since 4.4.0
*
* @param string $key Header name.
* @param string $value Header value, or list of values.
*/
public function set_header( $key, $value ) {
$key = $this->canonicalize_header_name( $key );
$value = (array) $value;
$this->headers[ $key ] = $value;
}
// etc...
If you needed to access them directly, you would extend the class:
class MY_WP_REST_Request extends WP_REST_Request {
public $params;
// etc...
}
Though in my case, using the access methods provided is proving to be fine.
$request->get_body()
for thebody
property. – Sally CJ Commented Jan 27, 2022 at 4:34