Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#51293 closed feature request (fixed)

Add $option Parameter to wp_send_json, wp_send_json_success and wp_send_json_error

Reported by: eroraghav's profile eroraghav Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.6 Priority: normal
Severity: normal Version: 5.5.1
Component: General Keywords: has-patch commit has-dev-note
Focuses: Cc:

Description

I mostly use wp_send_json function(s) as they work perfectly with wp.ajax.post and wp.ajax.send functions. But there was one problem that i was not able to use JSON Constants (FLAGS ) https://www.php.net/manual/en/json.constants.php
So i made an edit to add this option parameter that will be used to add JSON Constant.

$option is Optional. Options to be passed to json_encode(). Default 0.

Attachments (2)

51293.diff (2.1 KB) - added by Hareesh Pillai 4 years ago.
Refreshed the patch against trunk. Also, changed the $option parameter in the PR to $options to maintain consistency with wp_json_encode.
51293.2.diff (2.1 KB) - added by garrett-eclipse 4 years ago.
Refresh to address CS alignment of @param descriptions.

Download all attachments as: .zip

Change History (12)

This ticket was mentioned in PR #528 on WordPress/wordpress-develop by Ero-Raghav.


4 years ago
#1

  • Keywords has-patch added; needs-refresh removed

I mostly use wp_send_json function(s) as they work perfectly with wp.ajax.post and wp.ajax.send functions. But there was one problem that i was not able to use [JSON Constants (FLAGS )] (https://www.php.net/manual/en/json.constants.php).
So i made an edit to add this option parameter that will be used to add JSON Constant.

$option is Optional. Options to be passed to json_encode(). Default 0.

[Ticket] (https://core.trac.wordpress.org/ticket/51293)

This ticket was mentioned in Slack in #accessibility by afercia. View the logs.


4 years ago

#3 @afercia
4 years ago

  • Focuses accessibility removed

Removing the accessibility focus as this issue doesn't seem related to web content accessibility.

#4 @SergeyBiryukov
4 years ago

  • Keywords 2nd-opinion removed
  • Milestone changed from Awaiting Review to 5.6
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

Hi there, welcome to WordPress Trac! Thanks for the ticket and the PR.

Since wp_json_encode() already has the $options parameter, I agree that being able to utilize it in these functions seems like a useful addition.

@Hareesh Pillai
4 years ago

Refreshed the patch against trunk. Also, changed the $option parameter in the PR to $options to maintain consistency with wp_json_encode.

@garrett-eclipse
4 years ago

Refresh to address CS alignment of @param descriptions.

#5 @garrett-eclipse
4 years ago

  • Keywords commit added

Thanks for the ticket & patch @eroraghav, and refresh @hareesh-pillai. Testing it looks good to me, applies clean and all existing tests run nicely. I did make one minor change in 51293.2.diff to fix the CS issue with the @param descriptions as they should all aligned off of the longest param.

@SergeyBiryukov would you care to give it a review/test, I wasn't sure if the change warranted unit tests?

#6 @SergeyBiryukov
4 years ago

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

In 49235:

General: Add $options parameter to JSON response functions:

  • wp_send_json()
  • wp_send_json_success()
  • wp_send_json_error()

This allows for customizing the options passed to json_encode().

Props eroraghav, hareesh-pillai, garrett-eclipse.
Fixes #51293.

#7 @davidbaumwald
4 years ago

  • Keywords needs-dev-note added

Marking for a small call-out on the Misc Dev Note for the new parameter added in 5.6.

Last edited 4 years ago by davidbaumwald (previous) (diff)

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


4 years ago

Note: See TracTickets for help on using tickets.