WP_REST_Server::json_error()protectedWP 4.4.0

Retrieves an appropriate error representation in JSON.

Note: This should only be used in WP_REST_Server::serve_request(), as it cannot handle WP_Error internally. All callbacks and other internal methods should instead return a WP_Error with the data set to an array that includes a 'status' key, with the value being the HTTP status to send.

Method of the class: WP_REST_Server{}

No Hooks.

Return

String. JSON representation of the error

Usage

// protected - for code of main (parent) or child class
$result = $this->json_error( $code, $message, $status );
$code(string) (required)
WP_Error-style code.
$message(string) (required)
Human-readable message.
$status(int)
HTTP status code to send.
Default: null

Changelog

Since 4.4.0 Introduced.

WP_REST_Server::json_error() code WP 6.4.3

protected function json_error( $code, $message, $status = null ) {
	if ( $status ) {
		$this->set_status( $status );
	}

	$error = compact( 'code', 'message' );

	return wp_json_encode( $error );
}