wp_http_supports()
Determines whether the current environment includes an HTTP transport that can execute a request with the specified capabilities.
The function is useful before making HTTP requests, when you need to check in advance whether the required capability is supported, for example SSL/TLS for HTTPS requests.
Inside, the check is passed to the Requests library via Requests::has_capabilities().
No Hooks.
Returns
true|false.
true— the HTTP transport supports the specified capabilities.false— there is no suitable HTTP transport for the specified capabilities.
Usage
wp_http_supports( $capabilities, $url );
- $capabilities(array)
An array of capabilities to check, or an array of arguments similar to
$argsfrom wp_remote_request().If an indexed array is passed, for example
['ssl'], it will be converted to['ssl' => true].An array of capabilities can be passed as a list, for example
['ssl']. This list will be converted into an associative array of the form['ssl' => true].Default:
[]- $url(string|null)
The request URL. If an HTTPS or SSL URL is specified, the function will add the
sslcapability to the check if it has not already been set in$capabilities.If you pass an HTTPS URL as the second parameter, the function will add a check for the
sslcapability itself if it is not yet specified in the$capabilitiesarray.Default:
null
Examples
#1 SSL support check
Checks whether the WordPress HTTP API can make SSL/TLS requests.
if ( wp_http_supports( [ 'ssl' ] ) ) {
echo 'SSL requests are supported.';
} else {
echo 'SSL requests are not supported.';
} #2 Checking HTTPS URL support before the request
If an HTTPS URL is passed as the second parameter, the ssl capability will be added automatically.
$url = 'https://api.wordpress.org/core/version-check/1.7/';
if ( wp_http_supports( [], $url ) ) {
$response = wp_remote_get( $url );
} #3 Transmission of capabilities via an associative array
This option is convenient to use when you need to explicitly specify which capabilities should be enabled.
$capabilities = [
'ssl' => true,
];
if ( wp_http_supports( $capabilities ) ) {
echo 'Suitable HTTP transport found.';
}
Changelog
| Since 3.2.0 | Introduced. |
wp_http_supports() wp http supports code WP 7.0.3
function wp_http_supports( $capabilities = array(), $url = null ) {
$capabilities = wp_parse_args( $capabilities );
$count = count( $capabilities );
// If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array.
if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) === $count ) {
$capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) );
}
if ( $url && ! isset( $capabilities['ssl'] ) ) {
$scheme = parse_url( $url, PHP_URL_SCHEME );
if ( 'https' === $scheme || 'ssl' === $scheme ) {
$capabilities['ssl'] = true;
}
}
return WpOrg\Requests\Requests::has_capabilities( $capabilities );
}