WP HOOKS
filtrod 4.4.0

rest_authentication_errors

Args 1Plik wp-includes/rest-api/class-wp-rest-server.phpDokumentacja WP ↗

Opis

Kontroluje dostęp do endpointów REST API.

Przykład użycia

add_filter( 'rest_authentication_errors', ... )php
add_filter( 'rest_authentication_errors', 'wpdocs_rest_auth' );

function wpdocs_rest_auth( $result ) {
	$uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '';

	if ( ! is_user_logged_in() && str_contains( $uri, '/wp-json/wpdocs/' ) ) {
		return new WP_Error(
			'rest_forbidden',
			__( 'Wymagane logowanie.', 'wpdocs' ),
			array( 'status' => 401 )
		);
	}

	return $result;
}

This is used to pass a WP_Error from an authentication method back to the API. Authentication methods should check first if they're being used, as multiple authentication methods can be enabled on a site (cookies, HTTP basic auth, OAuth). If the authentication method hooked in is not actually being attempted, null should be returned to indicate another authentication method should check instead. Similarly, callbacks should ensure the value is null before checking for errors. A WP_Error instance can be returned if an error occurs, and this should match the format used by API methods internally (that is, the status data should be used). A callback can return true to indicate that the authentication method was used, and it succeeded.

Parametry

ParametrTypOpis
$errors\WP_Error | null | trueWP_Error if authentication error occurred, null if authentication method wasn't used, true if authentication succeeded.

Wywołanie

Rejestracja callbackaphp
// rejestracja funkcji zwrotnej
add_filter( 'rest_authentication_errors', 'twoja_funkcja' );

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