Last active
February 27, 2019 22:41
-
-
Save iandunn/c91d7dcd82390b8251b6 to your computer and use it in GitHub Desktop.
Log WordPress REST API errors
This file contains hidden or 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
<?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