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: Filtering the REST API response by context to include only 'view' context
add_filter( 'rest_filter_response_by_context', function( $data, $context ) {
    if ( 'view' !== $context ) {
        return null;
    }
    return $data;
}, 10, 2 );
// Example 2: Filtering the REST API response by context to exclude 'edit' context
add_filter( 'rest_filter_response_by_context', function( $data, $context ) {
    if ( 'edit' === $context ) {
        return null;
    }
    return $data;
}, 10, 2 );
// Example 3: Customizing the REST API response based on different contexts
add_filter( 'rest_filter_response_by_context', function( $data, $context ) {
    if ( 'edit' === $context ) {
        // Modify the data for 'edit' context
        $data['custom_field'] = 'This is a custom field for edit context';
    } elseif ( 'view' === $context ) {
        // Modify the data for 'view' context
        $data['custom_field'] = 'This is a custom field for view context';
    }
    return $data;
}, 10, 2 );