pre_http_requestfilter-hookWP 2.9.0

Allows you to short-circuit an HTTP request and return a specified value.

If the filter returns anything other than false, the HTTP request is canceled and the returned value is used instead.

The filter must return one of the following:

  • array — an array with the keys headers, body, response, cookies, and filename. See WP_HTTP_Requests_Response::to_array().

  • WP_Error — an instance of WP_Error.

  • false — the default. Do not short-circuit the request; process it normally.

Returning any other value may cause unexpected behavior.

Usage

add_filter( 'pre_http_request', 'wp_kama_pre_http_request_filter', 10, 3 );

/**
 * Function for `pre_http_request` filter-hook.
 * 
 * @param false|array|WP_Error $response    A preemptive return value of an HTTP request.
 * @param array                $parsed_args HTTP request arguments.
 * @param string               $url         The request URL.
 *
 * @return false|array|WP_Error
 */
function wp_kama_pre_http_request_filter( $response, $parsed_args, $url ){

	// filter...
	return $response;
}
$response(false|array|WP_Error)
Returning false lets the HTTP request run normally. Otherwise, the supplied value becomes the request result.
Default: false
$parsed_args(array)
HTTP request arguments. See wp_remote_request() or its wrappers for details about these arguments.
$url(string)
Request URL.

Examples

#1 Prevent requests from a particular plugin

Paid plugins commonly have their own update systems and send requests to their own sites to check for updates, licenses, or other data. Such requests may need to be canceled for various reasons. This code was created for a plugin that sent an HTTP update check on every admin page and made the admin area extremely slow due to a developer error.

add_filter( 'pre_http_request', 'requested_domain_update_disabled', 9, 3 );

/**
 * @param false|array|WP_Error $response    A preemptive return value of an HTTP request. Default false.
 * @param array                $parsed_args HTTP request arguments.
 * @param string               $url         The request URL.
 *
 * @return false|array|WP_Error
 */
function requested_domain_update_disabled( $response, $parsed_args, $url ) {
	global $pagenow;

	// Not the target request.
	if ( str_contains( $url, 'api.requested-domain.com' ) ) {
		return $response;
	}

	$is_ajax = wp_doing_ajax();
	$is_rest = defined( 'REST_REQUEST' ) && REST_REQUEST === true;
	$is_cron = wp_doing_cron();

	// Do not block HTTP requests during AJAX, REST, or cron requests.
	if ( $is_ajax || $is_rest || $is_cron ) {
		return $response;
	}

	// Do not block HTTP requests on the Plugins and Updates pages.
	if ( in_array( $pagenow, [ 'plugins.php', 'update-core.php' ] ) ) {
		return $response;
	}

	// Do not block HTTP requests on the plugin's own page.
	if ( isset( $_GET['page'] ) && str_contains( $_GET['page'], 'requested-plugin' ) ) {
		return $response;
	}

	// Return an empty request result in every other case.
	return [
		'headers'  => [],
		'body'     => '',
		'response' => [],
		'cookies'  => [],
	];
	// Or:
	// return ( new WP_HTTP_Requests_Response( new \WpOrg\Requests\Response() ) )->to_array();
}

Changelog

Since 2.9.0 Introduced.

Where the hook is called

WP_Http::request()
pre_http_request
wp-includes/class-wp-http.php 277
$pre = apply_filters( 'pre_http_request', false, $parsed_args, $url );

Where the hook is used in WordPress

Usage not found.