[wp-trac] [WordPress Trac] #39400: Add filter for remote request URL

WordPress Trac noreply at wordpress.org
Tue Dec 27 00:32:19 UTC 2016


#39400: Add filter for remote request URL
-------------------------+------------------------------
 Reporter:  iandunn      |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  HTTP API     |     Version:
 Severity:  minor        |  Resolution:
 Keywords:  has-patch    |     Focuses:
-------------------------+------------------------------

Comment (by dd32):

 Personally I find it weird that we have the `http_request_args` filter,
 and don't really see adding a `http_request_url` filter 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.NET
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39400#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list