You can use the status_header() function in WordPress, Function Reference / Status Header
What do you mean by custom code?
[StackOverflow Thread] You can return custom 4XX error codes (preferably those that are unassigned) for your own application’s error conditions – that is, 2xx for success, 4xx for Client error, etc
List of HTTP Status Codes
Can you explain a little better what your end goal is?
Here is a useful article for HTTP Status Codes – RFC 2616
HTTP status codes are extensible. HTTP applications are not required to understand the meaning of all registered status codes, though such understanding is obviously desirable. However, applications MUST understand the class of any status code, as indicated by the first digit, and treat any unrecognized response as being equivalent to the x00 status code of that class, with the exception that an unrecognized response MUST NOT be cached. For example, if an unrecognized status code of 431 is received by the client, it can safely assume that there was something wrong with its request and treat the response as if it had received a 400 status code.
-
This reply was modified 9 years, 7 months ago by
James. Reason: Updated content
Hi,
What do you mean by custom code?
I will try to explain …
I have registered the following route, which calls the function “ajax_Interface”:
register_rest_route( 'blabla/v1', '/ajax-interface', array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this,'ajax_interface' ),
'permission_callback' => function () {
return current_user_can( 'administrator' );
}
));
The function “ajax_interface” does some work. For example I do a SQL-update …
function ajax_interface() {
$db_update_result = $wpdb->update( ... SQL update ...
if ( $db_update_result > 0) {
error! - Give error status code back to JS
} else {
success!
}
return ( $ajax_callback_array );
}
If the SQL-update is not OK it should give a status code back. I want to react on error with JS:
error: function(data) {
// if error occured
}
As mentioned the function “status_header()” seems not to be working with REST API like above. I used it with admin-ajax before. Are you sure it is working? Have you done it already?
Thank you