WP_HTTP_Requests_Hooks
Source: wp-includes/class-wp-http-requests-hooks.php
Since: 4.7.0
Extends: WpOrgRequestsHooks
Purpose
Bridge between the Requests library’s internal hook system and WordPress’s action/filter system. Every outbound HTTP request made through WP_Http::request() gets an instance of this class as $options['hooks'] (set at class-wp-http.php ~line 345: 'hooks' => new WP_HTTP_Requests_Hooks( $url, $parsed_args )).
When Requests dispatches internal hooks (e.g., request.progress, curl.before_send), they flow through this class’s dispatch() method, which:
- Calls the parent
WpOrgRequestsHooks::dispatch()(fires any directly-registered callbacks) - Maps specific hooks to WordPress back-compat actions
- Fires a generic WordPress action
"requests-{$hook}"for every hook
Class Definition
#[AllowDynamicProperties]
class WP_HTTP_Requests_Hooks extends WpOrgRequestsHooksProperties
| Property | Type | Visibility | Description |
|---|---|---|---|
$url | string | protected | The URL being requested |
$request | array | protected | Request data in WP_Http format (the $parsed_args array from WP_Http::request()) |
Constructor
public function __construct( string $url, array $request )Stores the URL and request args for later use in dispatch(). Does not call parent::__construct() — the parent class (WpOrgRequestsHooks) has no constructor logic (it initializes $hooks = [] as a property default).
Methods
dispatch( $hook, $parameters = array() )
Returns: bool — True if hooks were run, false if nothing was hooked.
Flow:
parent::dispatch( $hook, $parameters )— Fires any callbacks registered directly on thisHooksinstance via$hooks->register(). This is howrequest.progresscallbacks work — they’re registered directly on the hooks object, NOT through WordPress’sadd_action().Back-compat switch — Currently handles one case:
'curl.before_send': Fires the WordPress action'http_api_curl'with args[ &$curl_handle, $this->request, $this->url ]. This is documented inclass-wp-http-curl.php(the deprecated transport).
Generic WordPress action — Fires
do_action_ref_array( "requests-{$hook}", $parameters, $this->request, $this->url )for every hook. The action name uses hyphens (not underscores), with a phpcs ignore comment forWordPress.NamingConventions.ValidHookName.UseUnderscores.
parent::dispatch( $hook, $parameters ) — Fires any callbacks registered directly on this Hooks instance via $hooks->register(). This is how request.progress callbacks work — they’re registered directly on the hooks object, NOT through WordPress’s add_action().
Available Hooks Dispatched Through This Class
Back-compat switch — Currently handles one case:
| Hook Name | WordPress Action | When It Fires | Parameters |
|---|---|---|---|
requests.before_request | requests-requests.before_request | Before transport sends request | [&$url, &$headers, &$data, &$type, &$options] |
request.progress | requests-request.progress | Per chunk during body reception | [$data, $bytes_so_far, $byte_limit] |
curl.before_send | requests-curl.before_send + http_api_curl (back-compat) | After cURL handle is configured, before curl_exec() | [&$curl_handle] |
curl.before_multi_add | requests-curl.before_multi_add | Before adding handle to multi stack | [&$curl_handle] |
curl.before_multi_exec | requests-curl.before_multi_exec | Before curl_multi_exec() | [&$curl_multi_handle] |
curl.after_multi_exec | requests-curl.after_multi_exec | After curl_multi_exec() | [&$curl_multi_handle] |
fsockopen.before_send | requests-fsockopen.before_send | Before writing to socket | [&$handle] |
fsockopen.after_send | requests-fsockopen.after_send | After writing to socket | [&$fake_headers] |
requests.before_parse | requests-requests.before_parse | After transport completes, before response parsing | [&$response, $url, $headers, $data, $type, $options] |
requests.after_request | requests-requests.after_request | After full response parsing | [&$return, $headers, $data, $type, $options] |
requests.before_redirect_check | requests-requests.before_redirect_check | Before redirect evaluation | [&$return, $headers, $data, $type, $options] |
Streaming Significance
Generic WordPress action — Fires do_action_ref_array( "requests-{$hook}", $parameters, $this->request, $this->url ) for every hook. The action name uses hyphens (not underscores), with a phpcs ignore comment for WordPress.NamingConventions.ValidHookName.UseUnderscores.
'curl.before_send': Fires the WordPress action'http_api_curl'with args[ &$curl_handle, $this->request, $this->url ]. This is documented inclass-wp-http-curl.php(the deprecated transport).
Direct Registration vs WordPress Actions
parent::dispatch( $hook, $parameters ) — Fires any callbacks registered directly on this Hooks instance via $hooks->register(). This is how request.progress callbacks work — they’re registered directly on the hooks object, NOT through WordPress’s add_action().
Direct registration (recommended for streaming):
php// Register directly on the hooks object $hooks = new WP_HTTP_Requests_Hooks( $url, $args ); $hooks->register( 'request.progress', $callback ); // Pass as $options['hooks']WordPress action (less useful):
phpadd_action( 'requests-request.progress', function( $data, $bytes, $limit ) { // This fires for ALL requests — no URL filtering possible // Parameters are NOT passed by reference in the WordPress action }, 10, 3 );
Back-compat switch — Currently handles one case:
Why This Class Matters for Streaming Architecture
Generic WordPress action — Fires do_action_ref_array( "requests-{$hook}", $parameters, $this->request, $this->url ) for every hook. The action name uses hyphens (not underscores), with a phpcs ignore comment for WordPress.NamingConventions.ValidHookName.UseUnderscores.
@since 4.7.0