WordPress.org

Make WordPress Core

Opened 16 months ago

Closed 7 months ago

#35666 closed enhancement (fixed)

Add support for HTTP response codes to wp_send_json_error(), wp_send_json_success(), and wp_send_json

Reported by: johnbillion Owned by: johnbillion
Milestone: 4.7 Priority: normal
Severity: normal Version: 3.5
Component: Formatting Keywords: has-patch commit
Focuses: Cc:

Description

wp_send_json_error() is the JSON-encoded equivalent of wp_die(). It should therefore support the sending of an HTTP response code in a similar way so a more appropriate response code can be set for error conditions. Currently, the response code needs to be set manually, otherwise it defaults to 200.

Attachments (5)

35666.diff (2.2 KB) - added by stephenharris 14 months ago.
Adds support for a 'status_code' parameter to wp_send_json(_error|_success)
35666-2.diff (2.2 KB) - added by stephenharris 14 months ago.
Alternative patch for allowing a header status to be set
35666.3.diff (1.9 KB) - added by westonruter 9 months ago.
35666.4.diff (456 bytes) - added by stevenkword 7 months ago.
Refresh after Changeset 38576
35666.2.diff (739 bytes) - added by johnbillion 7 months ago.

Download all attachments as: .zip

Change History (30)

#1 @ocean90
14 months ago

#36261 was marked as a duplicate.

@stephenharris
14 months ago

Adds support for a 'status_code' parameter to wp_send_json(_error|_success)

#2 @stephenharris
14 months ago

  • Keywords has-patch 2nd-opinion added; needs-patch removed

@stephenharris
14 months ago

Alternative patch for allowing a header status to be set

#3 @johnbillion
9 months ago

  • Keywords 2nd-opinion removed
  • Milestone changed from Awaiting Review to 4.7

#4 @johnbillion
9 months ago

  • Owner set to johnbillion
  • Resolution set to fixed
  • Status changed from new to closed

In 38422:

Formatting: Add a parameter to wp_send_json_error(), wp_send_json_success(), and wp_send_json() for specifying the HTTP response code.

Defaults to 200 in all cases, but can be used, for example, to return a 403 when using wp_send_json_error().

Fixes #35666
Props stephenharris

#5 @westonruter
9 months ago

In 38478:

Customize: Use new $status_code parameter for wp_send_json_error() instead of calling status_header() separately.

Props johnbillion.
See #35666.
Fixes #37897.

@westonruter
9 months ago

#6 @westonruter
9 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

I've found a defect with the changes here. Namely, if status_header() was being called previously to wp_send_json(), then the default $status_header of 200 would clobber the status_header() that was first set. In other words, this breaks backwards compatibility.

On a related note, I also found that _ajax_wp_die_handler was clobbering the status_header() call if output buffering is on (since headers_sent() would be false). See 35666.3.diff.

#7 @johnbillion
9 months ago

  • Keywords needs-testing added

Good catch.

#8 @johnbillion
9 months ago

In 38576:

Formatting: Don't send an HTTP status code in wp_send_json() by default. This avoids clobbering an HTTP status code that may have been set prior to calling this function.

Props westonruter
See #35666

#9 @johnbillion
9 months ago

The output buffering issue needs a bit more testing I think.

#10 @johnbillion
9 months ago

  • Keywords needs-refresh added

This ticket was mentioned in Slack in #core by stevenkword. View the logs.


8 months ago

#12 @stevenkword
8 months ago

@johnbillion -- Do you think you'd like to take a stab at the refresh, or would you like some fresh eyes on this?

This ticket was mentioned in Slack in #core by chriscct7. View the logs.


8 months ago

#14 @stevenkword
8 months ago

  • Owner changed from johnbillion to stevenkword
  • Status changed from reopened to reviewing

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


8 months ago

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


7 months ago

This ticket was mentioned in Slack in #core by desrosj. View the logs.


7 months ago

@stevenkword
7 months ago

Refresh after Changeset 38576

#18 @stevenkword
7 months ago

  • Keywords needs-refresh removed

Patch 35666.4.diff updated after 38576, but still needs testing on the output buffering.

Last edited 7 months ago by stevenkword (previous) (diff)

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


7 months ago

@johnbillion
7 months ago

#20 @johnbillion
7 months ago

35666.2.diff tackles this by always telling wp_die() not to set an HTTP status code header.

With output buffering on, this means a status set by wp_send_json() doesn't get clobbered. With output buffering off, the functionality doesn't change because headers_sent() in _ajax_wp_die_handler() is always true (due to the JSON echoed in wp_send_json()) so the status header doesn't get set anyway.

@westonruter Wanna give this a whirl?

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


7 months ago

#22 @westonruter
7 months ago

  • Keywords commit added; needs-testing removed

@johnbillion works for me!

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


7 months ago

#24 @johnbillion
7 months ago

  • Owner changed from stevenkword to johnbillion
  • Status changed from reviewing to accepted

#25 @johnbillion
7 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 38956:

Formatting: Don't overwrite the status header for Ajax responses that use output buffering or otherwise set their headers early.

Fixes #35666

Note: See TracTickets for help on using tickets.