WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 6 weeks ago

#24726 new enhancement

New filter hook for get_metadata()

Reported by: JD55 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords: has-patch
Focuses: Cc:

Description

The get_metadata() function has one filter hook: get_{$meta_type}_metadata. While this filter can be used to override or short-circuit the return of get_metadata(), it can't easily be used to filter the meta value. In the options API, get_option() has two hooks: pre_option_$option and option_$option. The get_{$meta_type}_metadata corresponds to the former, but it would be useful to have something similar to option_$option that will filter the actual meta value. Maybe get_{$meta_type}_metadata-{$meta_key}?

Attachments (4)

24726.patch (1.1 KB) - added by JD55 2 years ago.
24726.2.diff (1.9 KB) - added by MikeHansenMe 4 months ago.
Refresh and cleanup
24726.3.diff (2.6 KB) - added by MikeHansenMe 7 weeks ago.
Added Docs and Refresh
24726.4.diff (3.7 KB) - added by jtsternberg 6 weeks ago.
Add new filter

Download all attachments as: .zip

Change History (17)

@JD552 years ago

comment:1 @JD552 years ago

  • Keywords has-patch added

Added patch with filter get_{$meta_type}_metadata-{$meta_key}.

comment:2 @nacin18 months ago

  • Component changed from General to Options and Meta

comment:3 @sc0ttkclark17 months ago

As the lead developer of the Pods project, I would also find this useful so we could only cover the fields that the user creates, instead of loading up the Pod config and checking if the field exists for that meta_key.

@MikeHansenMe4 months ago

Refresh and cleanup

comment:4 @MikeHansenMe7 weeks ago

  • Keywords needs-docs added

@MikeHansenMe7 weeks ago

Added Docs and Refresh

comment:5 @MikeHansenMe7 weeks ago

  • Keywords needs-docs removed

comment:6 @jtsternberg6 weeks ago

Hmm, wouldn't it be preferable to put the new filter up above/below the existing "get_{$meta_type}_metadata" filter? [Like this](https://github.com/WebDevStudios/CMB2/blob/master/includes/CMB2_Field.php#L191-L227).

comment:7 @sc0ttkclark6 weeks ago

+1 on specificity before catch-all filter (meta key filter first)

comment:8 @MikeHansenMe6 weeks ago

I think one difference is that it is passing the meta_value into the filter which is not set until later in the function.

comment:9 @jtsternberg6 weeks ago

Ah, my fault, the purpose of the ticket is to provide a filter on the retrieved value, but I think it also makes sense to have a pre_get filter above the catchall.

@jtsternberg6 weeks ago

Add new filter

comment:10 @jtsternberg6 weeks ago

Updated patch which includes another pre-get filter with the meta-key as a dynamic portion of the hook name (and only if they passed a meta-key), and changed the actual value filter name to "{$meta_type}_metadata_value" since its actual purpose is to filter the value. I also removed the dynamic meta-key from the hook (and instead passed it as a parameter) at that point since we are not guaranteed to have one.

comment:11 @MikeHansenMe6 weeks ago

@jtsternberg, couldn't you just hook on get_{$meta_type}_metadata and check the meta_key that is passed in? Seems to accomplish the same without the additional filter.

comment:12 @jtsternberg6 weeks ago

I think @sc0ttkclark may weigh in as well, but there are many reasons why that would be valuable. Also, it follows precedence with the "pre_option_{$option}" filter: https://github.com/WordPress/WordPress/blob/master/wp-includes/option.php#L45

comment:13 @MikeHansenMe6 weeks ago

yea, thought about the similar implementations after I posted. Makes sense.

Note: See TracTickets for help on using tickets.