Opened 11 months ago
Last modified 11 months ago
#64036 assigned feature request
Filters the value of metadata after it is retrieved.
| Reported by: | eslamelsherif | Owned by: | eslamelsherif |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Options, Meta APIs | Version: | 6.9 |
| Severity: | normal | Keywords: | has-patch |
| Cc: | Focuses: | php-compatibility |
Description
## Change
Extended the get_metadata() function to apply two levels of filters after retrieving metadata:
{$meta_type}_metadata → runs for all meta keys of a given object type (e.g. user_metadata).
{$meta_type}_metadata_{$meta_key} → runs for a specific meta key (e.g. user_metadata_nickname).
## Why
Aligns metadata handling with how get_option() works, which has both a general filter (option_{$option}) and the ability to target specific values.
Provides developers more granular control:
Use the general filter to adjust all metadata of a type.
Use the meta-key-specific filter to target one field only.
## Example usage
<?php // Affect all user metadata add_filter( 'user_metadata', function( $value, $user_id, $meta_key, $single ) { return $value === '' ? 'N/A' : $value; }, 10, 4 ); // Affect only the nickname field add_filter( 'user_metadata_nickname', function( $value, $user_id, $single ) { return 'SuperHero'; }, 10, 3 );
Change History (1)
This ticket was mentioned in PR #10028 on WordPress/wordpress-develop by @eslamelsherif.
11 months ago
#1
- Keywords has-patch added
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
## Change
Extended the get_metadata() function to apply two levels of filters after retrieving metadata:
{$meta_type}_metadata→ runs for all meta keys of a given object type (e.g. user_metadata).{$meta_type}_metadata_{$meta_key}→ runs for a specific meta key (e.g. user_metadata_nickname).## Why
Aligns metadata handling with how get_option() works, which has both a general filter (option_{$option}) and the ability to target specific values.
Provides developers more granular control:
Use the general filter to adjust all metadata of a type.
Use the meta-key-specific filter to target one field only.
## Example usage
{{{php
Affect all user metadata
add_filter( 'user_metadata', function( $value, $user_id, $meta_key, $single ) {
}, 10, 4 );
Affect only the nickname field
add_filter( 'user_metadata_nickname', function( $value, $user_id, $single ) {
}, 10, 3 );
}}}
ticket
Trac ticket: