Using WordPress ‘http_api_transports’ PHP filter

The http_api_transports WordPress PHP Filter allows you to modify the available HTTP transports and their order in WordPress.

Usage

add_filter('http_api_transports', 'your_custom_function', 10, 3);

function your_custom_function($transports, $args, $url) {
    // your custom code here

    return $transports;
}

Parameters

  • $transports (string[]): Array of available HTTP transports to check. Default array contains ‘curl’ and ‘streams’, in that order.
  • $args (array): HTTP request arguments.
  • $url (string): The URL to request.

More information

See WordPress Developer Resources: http_api_transports

Examples

Reorder Transports

Change the order of the transports to prioritize ‘streams’ over ‘curl’:

add_filter('http_api_transports', 'reorder_http_transports', 10, 3);

function reorder_http_transports($transports, $args, $url) {
    $transports = array('streams', 'curl');
    return $transports;
}

Remove ‘curl’ Transport

Remove ‘curl’ from the list of available transports:

add_filter('http_api_transports', 'remove_curl_transport', 10, 3);

function remove_curl_transport($transports, $args, $url) {
    $key = array_search('curl', $transports);
    if ($key !== false) {
        unset($transports[$key]);
    }
    return $transports;
}

Add a Custom Transport

Add a custom transport to the list of available transports:

add_filter('http_api_transports', 'add_custom_transport', 10, 3);

function add_custom_transport($transports, $args, $url) {
    $transports[] = 'your_custom_transport';
    return $transports;
}

Use ‘curl’ Transport for Specific Domain

Force ‘curl’ transport for requests to a specific domain:

add_filter('http_api_transports', 'force_curl_for_specific_domain', 10, 3);

function force_curl_for_specific_domain($transports, $args, $url) {
    if (strpos($url, 'example.com') !== false) {
        $transports = array('curl');
    }
    return $transports;
}

Prioritize ‘curl’ for POST Requests

Prioritize ‘curl’ transport for POST requests:

add_filter('http_api_transports', 'prioritize_curl_for_post_requests', 10, 3);

function prioritize_curl_for_post_requests($transports, $args, $url) {
    if (isset($args['method']) && $args['method'] == 'POST') {
        $transports = array('curl', 'streams');
    }
    return $transports;
}