Skip to content

Instantly share code, notes, and snippets.

@BretCameron
Last active March 25, 2019 13:00
Show Gist options
  • Save BretCameron/7d09d0ecb7e7c20226fc1676f62dfe98 to your computer and use it in GitHub Desktop.
Save BretCameron/7d09d0ecb7e7c20226fc1676f62dfe98 to your computer and use it in GitHub Desktop.
Restrict JSON data visible via WordPress's REST API
<?php // Filter REST data
function filter_rest_data( $data, $post, $request ) {
$_data = $data->data;
$params = $request->get_params();
if ( ! isset( $params['id'] ) ) {
unset( $_data['date'] );
unset( $_data['slug'] );
unset( $_data['date_gmt'] );
unset( $_data['modified'] );
unset( $_data['modified_gmt'] );
unset( $_data['guid'] );
unset( $_data['type'] );
};
$data->data = $_data;
return $data;
};
add_filter( 'rest_prepare_movies', 'filter_rest_data', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment