Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 405 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why can't I access the query params from WP_REST_Request?

#1
On my blog, I'm trying to create an endpoint in order to load more articles using ajax. However, the query string parameters don't seem to be passed down to my function.
Here's my code, all of it is in the `function.php` file:

add_action( 'rest_api_init', function () {
register_rest_route( 'blog', '/articles', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'load_more'
));
});

function load_more(WP_REST_Request $request) {
var_dump($request->has_valid_params());
var_dump($request->get_params());
var_dump($request);
}
And here is what this returns when I call `/wp-json/blog/articles/?lang=en&tag=test` :

bool(true)

array(0) {}

object(WP_REST_Request)#2259 (8) {
["method":protected]=>
string(3) "GET"
["params":protected]=>
array(6) {
["URL"]=>
array(0) {
}
["GET"]=>
array(0) {
}
["POST"]=>
array(0) {
}
["FILES"]=>
array(0) {
}
["JSON"]=>
NULL
["defaults"]=>
array(0) {
}
}
["body":protected]=>
string(0) ""
["route":protected]=>
string(14) "/blog/articles"
["attributes":protected]=>
array(6) {
["methods"]=>
array(1) {
["GET"]=>
bool(true)
}
["accept_json"]=>
bool(false)
["accept_raw"]=>
bool(false)
["show_in_index"]=>
bool(true)
["args"]=>
array(0) {
}
["callback"]=>
string(9) "load_more"
}
["parsed_json":protected]=>
bool(true)
["parsed_body":protected]=>
bool(false)
}
It's almost like the parameters were deleted from the request object before reaching my function.
Reply

#2
<br/>
The object properties are protected therefore it is not possible to get them using the standard method, even if you want to access you will confront a `Fatal Error`.</br />

I however tried to access values by first converting the object to array then loop through each item and get what is needed.<br />

WP_REST_Request $request;
$request2 = (array)$request;
foreach($request2 as $req2) {
if (is_array($req2)) {
foreach($req2 as $req2_k => $req2_v) {
if ($req2_k == 'cpt') {
echo $req2_v;
}
}
}
}

As you can see I wanted to obtain `cpt` value from `attributes` array index, you need to adjust the loop to your liking.
Reply

#3
You can access query parameters via `WP_REST_Request::get_query_params()`:

```
$queryParams = $request->get_query_params();
$tag = $queryParams['tag'];
```

Alternatively you can use

```
$tag = $request->get_param('tag');
```

but that merges `$_POST`, `$_GET` and path parameters in that order
Reply

#4
If you pass some value via POST REQUEST you can access them by using `get_body_params()` method

add_action( 'rest_api_init', function () {
register_rest_route( "nh", "v2/slug", array(
'methods' => 'POST',
'callback' => 'my_awesome_func',
) );
} );



function my_awesome_func( $request ) {
$queryParams = $request->get_body_params();
return $queryParams;
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through