Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#38964 closed enhancement (fixed)

Add filter to allow modifying response *after* embedded data is adde

Reported by: loboyle's profile loboyle Owned by: jnylen0's profile jnylen0
Milestone: 4.8.1 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch fixed-major
Focuses: Cc:


The rest_post_dispatch is intended to be used for modifying responses, but the _embedded data is not yet available which limits the manipulations that can be done. It would be useful to have another filter that allows us to modify the final result being sent back, immediately before it's encoded and echoed.

For context, this is a problem for me in my wp-rest-jmespath plugin, which allows applying a JMESPath transform to a response to limit/rewrite the fields (see It is possible to work around this limitation by adding a handler for rest_pre_serve_request and copying the logic that would embed the data (e.g. but this is a fragile workaround - any changes to the logic for processing and sending after rest_pre_serve_request would not be picked up by the plugin and so responses may end up differing unexpectedly.

Attachments (2)

38964.diff (1001 bytes) - added by jnylen0 7 years ago.
38964.2.diff (1.0 KB) - added by jnylen0 7 years ago.

Download all attachments as: .zip

Change History (15)

#1 @joehoyle
7 years ago

I also had to do something similar in though I wasn't much duplicated code, so I don't think it was a bad solution.

#2 @loboyle
7 years ago

Yes, yours requires less duplicated code because for your use case you can avoid sending the response yourself and leave WP to do that (and the actual embedding). I need to copy the logic for sending the request (embed, json encode, check for encoding errors, check for jsonp callback, output) which is a bit more involved - still not massive, but definitely more fragile than having a final filter to allow modifying the result.

#3 @jnylen0
7 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

I think a rest_post_embed filter right in between $this->response_to_data and wp_json_encode is a good idea. We haven't needed this yet on, but we probably will.

This filter wouldn't work for Joe's example of adding granularity to embeds. This should be handled in core instead, though - see #35613.

See also #38131 regarding excluding specific fields from responses.

#4 @loboyle
7 years ago

Yes, that would suit perfectly, thanks. Filtering responses (e.g. as discussed in #38131) is why I put together my plugin, JMESPath allows a very powerful and flexible way of processing responses - not just about response size by dropping fields, but additional filtering, renaming, combining etc as well.

7 years ago

#5 @jnylen0
7 years ago

  • Keywords has-patch added; needs-patch removed
  • Milestone changed from Future Release to 4.8.1
  • Owner set to jnylen0
  • Status changed from new to accepted

Added a rest_post_embed filter in 38964.diff. This will address the case described in this ticket. See #41159 for an additional, semi-related enhancement.

This ticket was mentioned in Slack in #core-restapi by jnylen. View the logs.

7 years ago

7 years ago

#7 @jnylen0
7 years ago

In 38964.2.diff, changes per Slack feedback from @pento:

  • Rename to rest_pre_echo_request - rest_post_embed isn't the best name as this filter will be run even if no embedded requests are inserted
  • Fix documentation of $result parameter - at this point this is just a data array, not a WP_REST_Response object
Version 0, edited 7 years ago by jnylen0 (next)

#8 @loboyle
7 years ago

Great, thanks @jnylen0.

#9 @jnylen0
7 years ago

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

In 40961:

REST API: Add a filter to allow modifying the response *after* embedded data is added.

Fixes #38964.

#10 @jnylen0
7 years ago

  • Keywords fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for backport to 4.8 branch.

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

7 years ago

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

7 years ago

#13 @westonruter
7 years ago

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

In 41093:

REST API: Add a filter to allow modifying the response *after* embedded data is added.

Merges [40961] onto 4.8 branch.
Props jnylen0.
Fixes #38964 for 4.8.1.

Note: See TracTickets for help on using tickets.