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:

  1. Calls the parent WpOrgRequestsHooks::dispatch() (fires any directly-registered callbacks)
  2. Maps specific hooks to WordPress back-compat actions
  3. Fires a generic WordPress action "requests-{$hook}" for every hook

Class Definition

php
#[AllowDynamicProperties]
class WP_HTTP_Requests_Hooks extends WpOrgRequestsHooks

Properties

PropertyTypeVisibilityDescription
$urlstringprotectedThe URL being requested
$requestarrayprotectedRequest data in WP_Http format (the $parsed_args array from WP_Http::request())

Constructor

php
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:

  1. 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().

  2. 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 in class-wp-http-curl.php (the deprecated transport).
  3. 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.

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 NameWordPress ActionWhen It FiresParameters
requests.before_requestrequests-requests.before_requestBefore transport sends request[&$url, &$headers, &$data, &$type, &$options]
request.progressrequests-request.progressPer chunk during body reception[$data, $bytes_so_far, $byte_limit]
curl.before_sendrequests-curl.before_send + http_api_curl (back-compat)After cURL handle is configured, before curl_exec()[&$curl_handle]
curl.before_multi_addrequests-curl.before_multi_addBefore adding handle to multi stack[&$curl_handle]
curl.before_multi_execrequests-curl.before_multi_execBefore curl_multi_exec()[&$curl_multi_handle]
curl.after_multi_execrequests-curl.after_multi_execAfter curl_multi_exec()[&$curl_multi_handle]
fsockopen.before_sendrequests-fsockopen.before_sendBefore writing to socket[&$handle]
fsockopen.after_sendrequests-fsockopen.after_sendAfter writing to socket[&$fake_headers]
requests.before_parserequests-requests.before_parseAfter transport completes, before response parsing[&$response, $url, $headers, $data, $type, $options]
requests.after_requestrequests-requests.after_requestAfter full response parsing[&$return, $headers, $data, $type, $options]
requests.before_redirect_checkrequests-requests.before_redirect_checkBefore 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 in class-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().

  1. 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']
    
  2. WordPress action (less useful):

    php
    add_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