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 to include only specific fields for logged-in users
function custom_rest_filter_response_by_context( $data, $post, $context ) {
if ( 'view' === $context && is_user_logged_in() ) {
$data['title'] = $post->post_title;
$data['content'] = $post->post_content;
}
return $data;
}
add_filter( 'rest_filter_response_by_context', 'custom_rest_filter_response_by_context', 10, 3 );
// Example 2: Filter response to include additional meta data for administrators
function custom_rest_filter_response_by_context( $data, $post, $context ) {
if ( 'edit' === $context && current_user_can( 'administrator' ) ) {
$data['meta'] = get_post_meta( $post->ID );
}
return $data;
}
add_filter( 'rest_filter_response_by_context', 'custom_rest_filter_response_by_context', 10, 3 );
// Example 3: Filter response to exclude certain fields for public users
function custom_rest_filter_response_by_context( $data, $post, $context ) {
if ( 'view' === $context && !is_user_logged_in() ) {
unset( $data['author'] );
unset( $data['featured_image'] );
}
return $data;
}
add_filter( 'rest_filter_response_by_context', 'custom_rest_filter_response_by_context', 10, 3 );