WP HOOKS
filtrod 2.9.0

pre_http_request

Args 3Plik wp-includes/class-wp-http.phpDokumentacja WP ↗

Opis

Pozwala przechwycić żądanie HTTP przed jego wykonaniem — do blokowania, mockowania lub cache’owania odpowiedzi.

Przykład użycia

add_filter( 'pre_http_request', ... )php
add_filter( 'pre_http_request', 'wpdocs_block_external', 10, 3 );

function wpdocs_block_external( $preempt, $args, $url ) {
	if ( false === strpos( $url, home_url() ) ) {
		return new WP_Error( 'blocked', __( 'Żądania zewnętrzne są zablokowane.', 'wpdocs' ) );
	}

	return $preempt;
}

Returning a non-false value from the filter will short-circuit the HTTP request and return early with that value. A filter should return one of:

  • An array containing 'headers', 'body', 'response', 'cookies', and 'filename' elements
  • A WP_Error instance
  • Boolean false to avoid short-circuiting the response Returning any other value may result in unexpected behavior.

Parametry

ParametrTypOpis
$responsefalse | array | \WP_ErrorA preemptive return value of an HTTP request. Default false.
$parsed_argsarrayHTTP request arguments.
$urlstringThe request URL.

Wywołanie

Rejestracja callbackaphp
// rejestracja funkcji zwrotnej — podaj liczbę argumentów!
add_filter( 'pre_http_request', 'twoja_funkcja', 10, 3 );

function twoja_funkcja($param_1, $param_2, $param_3) {
	// Twój kod
}

Popularne w tej kategorii