WP_REST_Server::json_error() WordPress Method

The WP_REST_Server::json_error() method is used to return a JSON error response. This is useful for AJAX requests where the response needs to be in JSON format.

WP_REST_Server::json_error( string $code, string $message, int $status = null ) #

Retrieves an appropriate error representation in JSON.


Description

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.


Top ↑

Parameters

$code

(string)(Required)WP_Error-style code.

$message

(string)(Required)Human-readable message.

$status

(int)(Optional) HTTP status code to send.

Default value: null


Top ↑

Return

(string) JSON representation of the error


Top ↑

Source

File: wp-includes/rest-api/class-wp-rest-server.php

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

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

		return wp_json_encode( $error );
	}


Top ↑

Changelog

Changelog
VersionDescription
4.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.