Opened 10 years ago
Closed 7 years ago
#39400 closed enhancement (wontfix)
Add filter for remote request URL
| Reported by: | iandunn | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | HTTP API | Version: | |
| Severity: | minor | Keywords: | has-patch close 2nd-opinion reporter-feedback |
| Cc: | Focuses: |
Description
Currently, I can filter an HTTP request's parameters, but not its URL.
I've run into several situations where this would be useful over the years, and it seems like others have as well.
There are a few workarounds, but they're inconvenient and ugly. This should be simple, and it seems like something developers just assume already exists.
Attachments (1)
Change History (5)
Note:
See TracTickets
for help on using tickets.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Personally I find it weird that we have the
http_request_argsfilter, and don't really see adding ahttp_request_urlfilter in addition to it a good idea.Filtering the URL requested is currently possible, but it's not as straightforward as some might expect - which is IMHO a good thing, as it also forces you to handle more of the actual request logic.
To filter the URL, you can hook into
pre_http_request, match the URL, and then return the result of the request you'd like to make.For example:
add_filter( 'pre_http_request', function( $preempt, $r, $url ) { if ( 'example.com' == parse_url( $url, PHP_URL_HOST ) ) { // mangle $r here to add extra headers, etc. // override the current request with the new one: $preempt = wp_remote_request( 'http://example.NET/', $r ); } return $preempt; }, 10, 3 ); wp_remote_request( 'http://example.com/' ); // returns result from example.NETEdit: I should note that this is more or less what's shown in one of your links; what the link doesn't show is how clean and straightforward the filter can be when used correctly.