Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Created February 20, 2018 22:44
Show Gist options
  • Save wpscholar/f93b64a21d52059b4691ecd0273d162a to your computer and use it in GitHub Desktop.
Save wpscholar/f93b64a21d52059b4691ecd0273d162a to your computer and use it in GitHub Desktop.
Use the WordPress REST API directly in PHP without actually making an HTTP request.
<?php
function wpscholar_rest_get_request( $route, array $params = [] ) {
$request = new WP_REST_Request( 'GET', $route );
$request->set_query_params( $params );
$response = rest_do_request( $request );
return rest_get_server()->response_to_data( $response, false );
}
function wpscholar_rest_post_request( $route, array $params = [] ) {
$request = new WP_REST_Request( 'POST', $route );
$request->set_body_params( $params );
$response = rest_do_request( $request );
return rest_get_server()->response_to_data( $response, false );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment