Last active
November 4, 2020 21:34
-
-
Save lynt-smitka/2e61c7eb545ab6a162fbc57f17b3adae to your computer and use it in GitHub Desktop.
Remove sensitive user's data from the REST API response in WP
This file contains 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
//Remove sensitive data from REST API | |
function lynt_remove_sensitive_data_from_rest( $response ) { | |
if(!current_user_can('list_users')){ | |
//get WP_REST_Response | |
$data = $response->get_data(); | |
//unset sensitive fields | |
if(preg_replace('/[\W]+/', '',$data['name']) == preg_replace('/[\W]+/', '',$data['slug'])) $data['name']="Author"; | |
unset($data['link']); | |
unset($data['slug']); | |
unset($data['avatar_urls']); | |
//set data back | |
$response->set_data($data); | |
} | |
return $response; | |
} | |
add_filter( 'rest_prepare_user', 'lynt_remove_sensitive_data_from_rest'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment