Opened 8 years ago
Closed 8 years ago
#38463 closed enhancement (wontfix)
WP_REST_Request allow getting a specific param value.
Reported by: | TimothyBlynJacobs | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.4 |
Component: | REST API | Keywords: | |
Focuses: | Cc: |
Description
It would be great if there was an easy way to get a specific parameter value.
For example, instead of:
$url_params = $request->get_url_params(); $value = $url_params['id'];
You could do either this
$value = $request->get_url_param( 'id' );
or this
$value = $request->get_param( 'id', 'URL' );
Change History (5)
#2
follow-up:
↓ 3
@
8 years ago
Not exactly, because that follows the parameter order. What I need is to be able to get a parameter from a specific location.
#3
in reply to:
↑ 2
@
8 years ago
- Keywords close added; reporter-feedback removed
Replying to TimothyBlynJacobs:
Not exactly, because that follows the parameter order. What I need is to be able to get a parameter from a specific location.
We specifically try to avoid this for a few reasons: namely, a JSON or URL-encoded POST body could be used for data, and we need to handle those equally, and defaults are applied by having those at the bottom of the stack.
This is intentional behaviour. The parameters are still available, but it's just a little tougher to access them to encourage using the stack. I could be missing something though. :)
#5
@
8 years ago
- Keywords close removed
- Milestone Awaiting Review deleted
- Resolution set to wontfix
- Status changed from new to closed
I'm going to close this as a wontfix. While there is definitely a use-case for getting specific arguments from one source, it's not something we want to encourage. The basic premise of the REST API is that it's resource-based, and conflicting data in the URL and body (JSON/POST data) is typically an anti-pattern as it indicates that your URL isn't really representing the resource. Worst case, you can always name your URL argument something different.
@TimothyBlynJacobs You should be able to do this already by setting the url parameter as an array key on the
WP_REST_Request
object. Example:$request['param']
or$request['id']
Does that meet your needs?