Skip to content

Instantly share code, notes, and snippets.

@iandunn
Last active February 27, 2019 22:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iandunn/c91d7dcd82390b8251b6 to your computer and use it in GitHub Desktop.
Save iandunn/c91d7dcd82390b8251b6 to your computer and use it in GitHub Desktop.
Log WordPress REST API errors
<?php
/**
* Log REST API errors
*
* @param WP_REST_Response $result Result that will be sent to the client.
* @param WP_REST_Server $server The API server instance.
* @param WP_REST_Request $request The request used to generate the response.
*/
function log_rest_api_errors( $result, $server, $request ) {
if ( $result->is_error() ) {
error_log( sprintf(
"REST request: %s: %s",
$request->get_route(),
print_r( $request->get_params(), true )
) );
error_log( sprintf(
"REST result: %s: %s",
$result->get_matched_route(),
print_r( $result->get_data(), true )
) );
}
return $result;
}
add_filter( 'rest_post_dispatch', 'log_rest_api_errors', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment