http_request_argsfilter-hookWP 2.7.0

Allows you to change HTTP API request parameters.

Usage

add_filter( 'http_request_args', 'wp_kama_http_request_args_filter', 10, 2 );

/**
 * Function for `http_request_args` filter-hook.
 * 
 * @param array  $parsed_args An array of HTTP request arguments.
 * @param string $url         The request URL.
 *
 * @return array
 */
function wp_kama_http_request_args_filter( $parsed_args, $url ){

	// filter...
	return $parsed_args;
}
$parsed_args(array)
The array of HTTP request parameters set for the current HTTP request.
$url(string)
The request URL.

Examples

#1 Disable SSL certificate verification for all HTTP requests

Disabling it can be useful for local development, so the code below first checks whether the current environment is local:

// Disable SSL verification for `wp_remote_*()` in a local environment.
if( 'local' === wp_get_environment_type() ){

	add_filter( 'http_request_args', function( $parsed_args ){

		$parsed_args['sslverify'] = false;

		return $parsed_args;
	} );

}

This code assumes that the WP_ENVIRONMENT_TYPE constant is used in wp-config.php to set the current development environment.

This code must run before the request itself is created through the HTTP API. The most convenient place is a must-use plugin. It can also be implemented as a regular plugin or simply added to the theme's functions.php because HTTP requests are generally made after the init event.

Changelog

Since 2.7.0 Introduced.

Where the hook is called

WP_Http::request()
http_request_args
wp-includes/class-wp-http.php 252
$parsed_args = apply_filters( 'http_request_args', $parsed_args, $url );

Where the hook is used in WordPress

wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php 723
add_filter( 'http_request_args', $limit_response_size );
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php 729
remove_filter( 'http_request_args', $limit_response_size );