Keep REST API Responses Out of Search Results
Sends an X-Robots-Tag: noindex header with every REST API response so search engines do not index raw JSON from /wp-json/ next to your real pages.
Opens WPIDE on
Code
add_filter( 'rest_post_dispatch', function ( $response ) {
if ( $response instanceof WP_REST_Response ) {
$response->header( 'X-Robots-Tag', 'noindex, nofollow' );
}
return $response;
} );
Notes
This does not block access to the API; it only asks search engines not to index the responses.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.