Skip to content

Instantly share code, notes, and snippets.

@mtrl
Last active November 4, 2016 12:43
Show Gist options
  • Save mtrl/0c873a0560f68b94452f86c643ccf90b to your computer and use it in GitHub Desktop.
Save mtrl/0c873a0560f68b94452f86c643ccf90b to your computer and use it in GitHub Desktop.
// Check for the existence of a header authentication token before dispatching any data
add_filter( 'rest_pre_dispatch', function() {
$requestPath = $_SERVER['REQUEST_URI'];
if($requestPath != '/wp-json/jwt-auth/v1/token') {
$tokenValidationResponse = apply_filters('validate_token', null);
if(gettype($tokenValidationResponse) == 'object' && get_class($tokenValidationResponse) == "WP_Error") {
foreach($tokenValidationResponse->error_data as $key => $error) {
$status = $error['status'];
$msg = $key;
};
return array(
'error' => array(
'msg' => $msg,
'code' => $status,
));
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment