Created
February 20, 2018 22:44
-
-
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.
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 | |
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