Created
April 9, 2019 19:05
-
-
Save ethangardner/c2ba6bfc4063c1407c99e583efdcd271 to your computer and use it in GitHub Desktop.
Log WordPress REST API Queries
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
<?php | |
/** | |
* Log REST API queries | |
* | |
* @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_queries( $result, $server, $request ) { | |
global $wpdb; | |
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES === true ) { | |
error_log( sprintf( | |
"REST request: %s: %s", | |
$request->get_route(), | |
print_r( $request->get_params(), true ) | |
) ); | |
error_log( sprintf( | |
"REST request: %s: %s", | |
$request->get_route(), | |
print_r( $wpdb->queries, true ) | |
) ); | |
} | |
return $result; | |
} | |
add_filter( 'rest_post_dispatch', 'log_rest_api_queries', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment