#11: http_response

The http_response filter makes it possible to manipulate the response array returned by a wp_remote_request()/WP_Http::request() call.

It is evaluated in the private WP_Http::_dispatch_request() method, which is called by WP_Http::request() in wp-includes/class-http.php file.

The http_response filter accepts three arguments:

  1. array $response The response array
  2. array $args An array of arguments (see wp_remote_request() in wp-includes/http.php)
  3. string $url The URL to request

Example:

Let’s say you’re pinging some external site and displaying the body of the requested site somewhere but you only want to display the content if it is actually valid — say, based on whether the status code is 200, 301, or 302. For any other status code, you want to override the body of the http response with some general template.

View the code example on Gist

Have a suggested improvement? You can fork the Gist and comment back with the link. If all is agreeable, I’ll merge in your changes. Crowd-sourced documentation FTW!

Leave a Reply

Your email address will not be published. Required fields are marked *