apply_filters( "get_{$meta_type}_metadata", mixed $value , int $object_id , string $meta_key , bool $single , string $meta_type )
Short-circuits the return value of a meta field.
Contents
Description
The dynamic portion of the hook name, $meta_type
, refers to the meta object type (post, comment, term, user, or any other type with an associated meta table).
Returning a non-null value will effectively short-circuit the function.
Possible filter names include:
get_post_metadata
get_comment_metadata
get_term_metadata
get_user_metadata
Parameters
-
$value
mixed -
The value to return, either a single metadata value or an array of values depending on the value of
$single
. Default null. -
$object_id
int -
ID of the object metadata is for.
-
$meta_key
string -
Metadata key.
-
$single
bool -
Whether to return only the first value of the specified
$meta_key
. -
$meta_type
string -
Type of object metadata is for. Accepts
'post'
,'comment'
,'term'
,'user'
, or any other object type with an associated meta table.
More Information
The filter must return null if the data should be taken from the database. If it returns anything else, the get_metadata() function (and therefore the get_user_meta
) will return what the filter returns.
Source
File: wp-includes/meta.php
.
View all references
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single, $meta_type );
Changelog
Version | Description |
---|---|
5.5.0 | Added the $meta_type parameter. |
3.1.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Note that although this filter filters the value that will be returned, you do not receive the current value as the first parameter.
You allways receive
null
in the first parameter.If your callback returns anything else than null, this is the value that the function will return at the end. If you return null, the function will continue to run normally and try to fetch the data from the database (or cache).
Top ↑
Feedback
Actually – you do not “always receive NULL” as the first parameter.While it is true that WordPress calls the first (as possibly ONLY) filter in the sequence with a NULL $value, you should allow for the possibility that a higher-priority filter could be passing down a $value to your filter!To play nice with other plug-ins, rather than your filter returning a hard-coded “null” if it decides to take no action, it may be appropriate for your filter to return the $value (which may be null, or may be the value injected by an earlier filter!) — By Andy Schmidt —
Example migrated from Codex:
Top ↑
Feedback
You shouldn’t return null if the checks don’t pass, return $value. otherwise another filter with higher priority will not be able to set the value:
add_filter( 'get_user_metadata', 'otherplugin_get_foo', 10, 4 ); function otherplugin_get_foo( $value, $object_id, $meta_key, $single ) { if ( 'foo' == $meta_key ) { return array( 'bar' ); } return $value; // CORRECT if $value is null it will return null, if another plugin has already used the same filter to set a value it will return that value; } add_filter( 'get_user_metadata', 'myplugin_get_baz', 10, 4 ); function otherplugin_get_foo( $value, $object_id, $meta_key, $single ) { if ( 'baz' == $meta_key ) { return array( 'bar' ); } return null; // WRONG! THIS WILL OVERRIDE ALL PREVIOUS get_{$meta_type}_metadata FILTERS; }
Now if you call get_user_meta(1, ‘foo’, true); you will always get null, because myplugin_get_baz will override the $value (bar) set by the first otherplugin_get_foo filter. Always return the first parameter if you’re not going to edit the value. — By gorgdesign —