WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#36261 closed enhancement (duplicate)

Allow a response code to be passed to wp_send_json_success, wp_send_json_error and wp_send_json

Reported by: stephenharris Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:
PR Number:

Description

Currently, regardless of whether you call wp_send_json_success() or wp_send_json_error() the HTTP header code is 200. The only difference between the two is a 'success' flag which takes a boolean value.

While this may be appropriate in some cases, it would be useful to be able to specify a response code. This is perhaps chiefly relevant to wp_send_json_error() and by consequently wp_send_json(), where the client expects error messages to have an appropriate error code (i.e. 4xx or 5xx). For example, Backbone allows success and error callbacks to be specified when making an ajax request and will invoke one or the other depending on the response code.

Currently this is worked around by checking the success flag in the success callback. Allowing a response code to be set means we can:

  1. Keep 'success' behaviour separate from 'error' behaviour as almost all HTTP frameworks will automatically handle responses differently depending on the response code.
  2. Remove duplication of 'error' behaviour (as we may still need to handle errors when a 200 response is returned. In Backbone this means a duplication of code in the success and error callbacks.

The response code would default to 200.

Change History (1)

#1 @ocean90
4 years ago

  • Component changed from REST API to General
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version trunk deleted

Duplicate of #35666.

Note: See TracTickets for help on using tickets.