Make WordPress Core

Opened 8 weeks ago

Last modified 8 weeks ago

#64036 assigned feature request

Filters the value of metadata after it is retrieved.

Reported by: eslamelsherif's profile eslamelsherif Owned by: eslamelsherif's profile eslamelsherif
Milestone: Awaiting Review Priority: normal
Severity: normal Version: trunk
Component: Options, Meta APIs Keywords: has-patch
Focuses: php-compatibility Cc:

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.


8 weeks ago
#1

  • Keywords has-patch added

## 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 );

}}}
ticket

Trac ticket:

Note: See TracTickets for help on using tickets.