Skip to content

Instantly share code, notes, and snippets.

@ethangardner
Created April 9, 2019 19:06
Show Gist options
  • Save ethangardner/a7d572c02eab66e9e0735dc52c54f46a to your computer and use it in GitHub Desktop.
Save ethangardner/a7d572c02eab66e9e0735dc52c54f46a 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