Random WordPress Function

Learn about a new WordPress function every day!


Function Signature:

rest_filter_response_by_context

Function Description:

Filters the response to remove any fields not available in the given context.

Function Examples:

⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Filter response data based on context in a custom REST route
add_filter( 'rest_filter_response_by_context', function( $data, $context ) {
    if ( 'view' === $context ) {
        // Modify data for view context
        $data['modified_field'] = 'value';
    }
    return $data;
}, 10, 2 );
// Example 2: Restrict certain fields from being shown in the response based on context
add_filter( 'rest_filter_response_by_context', function( $data, $context ) {
    if ( 'edit' === $context ) {
        // Remove sensitive data from the response in edit context
        unset( $data['sensitive_field'] );
    }
    return $data;
}, 10, 2 );
// Example 3: Customize response data for different contexts in a specific REST endpoint
add_filter( 'rest_filter_response_by_context', function( $data, $context ) {
    if ( 'embed' === $context ) {
        // Add additional data for embed context
        $data['additional_field'] = 'extra_value';
    }
    return $data;
}, 10, 2 );