Skip to content

Instantly share code, notes, and snippets.

@ethangardner
Created April 9, 2019 19:05
Show Gist options
  • Save ethangardner/c2ba6bfc4063c1407c99e583efdcd271 to your computer and use it in GitHub Desktop.
Save ethangardner/c2ba6bfc4063c1407c99e583efdcd271 to your computer and use it in GitHub Desktop.
Log WordPress REST API Queries
<?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