Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// Filter search endpoint responses.
add_filter( 'rest_post_dispatch', 'wp_47684_filter_response', 10, 3 );
/**
* Resolves the behavior reported in WP Core Trac 47684.
*
* @see https://core.trac.wordpress.org/ticket/47684
*
* @param WP_REST_Response $response The REST API response object.
* @param WP_REST_Server $server The REST API server instance handling the request.
* @param WP_REST_Request $request The REST API request object.
*
* @return WP_REST_Response
*/
function wp_47684_filter_response( WP_REST_Response $response, WP_REST_Server $server, WP_REST_Request $request ) : WP_REST_Response {
if ( '/wp/v2/search' !== $request->get_route() ) {
return $response;
}
foreach ( $response->data as $index => $result ) {
// Duplicate the `self` link as `original` to allow embedding.
if ( isset( $result['_links']['self'] ) ) {
$response->data[ $index ]['_links']['original'] = $result['_links']['self'];
}
// Make the `self` link non-embeddable.
if ( isset( $result['_links']['self'][0]['embeddable'] ) ) {
unset( $response->data[ $index ]['_links']['self'][0]['embeddable'] );
}
}
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.