Skip to content

Instantly share code, notes, and snippets.

@imath
Created September 2, 2019 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imath/00630617d731b51e4db4ef9f81752eb5 to your computer and use it in GitHub Desktop.
Save imath/00630617d731b51e4db4ef9f81752eb5 to your computer and use it in GitHub Desktop.
Testing external REST API requests
<?php
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
function test_wp_api_request() {
wp_enqueue_script( 'wp-api-request' );
wp_add_inline_script( 'wp-api-request', '
wp.apiRequest( {
url: \'https://imathi.eu/wp-json/wp/v2/posts\',
type: \'GET\',
data: {
context: \'view\',
search: \'core.trac.wordpress.org\'
}
} ).done( function( data ) {
console.log( data );
} ).fail( function( error ) {
console.log( error );
} );
' );
}
add_action( 'wp_enqueue_scripts', 'test_wp_api_request' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment