[wp-trac] [WordPress Trac] #46238: REST API: Allow conditional field registration

WordPress Trac noreply at wordpress.org
Tue Feb 12 00:42:00 UTC 2019


#46238: REST API: Allow conditional field registration
-------------------------+-----------------------------
 Reporter:  dmsnell      |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  REST API     |    Version:
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 `register_rest_field()` allows us to add additional fields to API call
 responses but it doesn't allow us to do so conditionally. in prior art we
 add a field and then remove it later when it's not needed.

 why would inclusion of a field be conditional? what is wrong with
 returning `null`?

 we came across this when wanting to add metadata to video files, which are
 `post` types and conflated with posts and other media attachments. we'd
 like to be able to just add fields to video file media requests and not
 pollute the responses for all of the other "subtypes." we'd also like to
 do it without resorting to surprising behaviors like adding and later
 removing them.

 in this ticket I'd like to propose two approaches to accomplishing this
 goal. the first is a bit clever but should make it possible to extend
 existing behaviors without breaking any code and without introducing
 further confusion on how to add fields. it uses a "sentinel value" passed
 into the `get_callback()` function which can be returned to indicate that
 the field should not exist.

 the second approach creates a new function
 `register_conditional_rest_field()` and provides a wrapped response format
 to indicate whether the field should be added. this approach is less
 idiomatic in my opinion as we're not used to returning wrapped values.

 ---

 I thought for sure I'd seen a discussion around this before but I couldn't
 find any tickets with my search. that being said I'm curious why this
 doesn't already exist as it seems like it could be a common need, though
 I'm not sure how the fact that media attachments are a bit odd in
 comparison to custom post types comes into play.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/46238>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list