Created
July 25, 2019 18:10
-
-
Save chrisvanpatten/6921ec090efd372d62b3ac9d0a7d497d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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