[wp-trac] [WordPress Trac] #43941: Add default value to register meta

WordPress Trac noreply at wordpress.org
Tue Jun 26 21:58:12 UTC 2018


#43941: Add default value to register meta
----------------------------------------+---------------------------
 Reporter:  spacedmonkey                |       Owner:  spacedmonkey
     Type:  enhancement                 |      Status:  assigned
 Priority:  normal                      |   Milestone:  5.0
Component:  Options, Meta APIs          |     Version:  4.6
 Severity:  normal                      |  Resolution:
 Keywords:  has-patch needs-unit-tests  |     Focuses:  rest-api
----------------------------------------+---------------------------
Changes (by flixos90):

 * owner:  (none) => spacedmonkey
 * status:  new => assigned


Comment:

 [attachment:43941.2.diff] is mostly on the right track IMO (filter names
 are good!). Just a few remarks:
 * Only one of the two filters should be applied. Look at `sanitize_meta()`
 for example to see how it handles those filters. I'd suggest checking `!
 empty( $object_subtype ) && has_filter(
 "default_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" )`, and if
 so only run the subtype-filter and return immediately. Otherwise, fall
 back to "default_{$object_type}_meta_{$meta_key}".
 * The function `get_metadata_default()` would be a lot simpler and more
 flexible if it only executed the filters and didn't touch `$wp_meta_keys`.
 The actual logic to get the registered default value should be added as a
 filter hook via `register_meta()` (and also removed again via
 `unregister_meta_key()`. This would be in line with how default values are
 registered for settings.
 * The argument for the default value should be called `default` instead of
 `default_value`. This is in line with how it works for settings.
 * `get_metadata_default()` as well as the two filters need to be aware of
 the value of `$single`, in order to allow developers full flexibility in
 handling this. The default value may need to be different, depending on
 how a meta value is requested.

 And two smaller detail improvements:
 * Instead of using `null|array|string` for the default value in docs, it
 should use `mixed`. A default may be anything, also an integer, float,
 bool or even object.
 * The docblock for the function needs proper third-person description, a
 since annotation, plus parameter and return value descriptions.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/43941#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list