Skip to content

Instantly share code, notes, and snippets.

@georgestephanis
Created August 16, 2017 22:28
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 georgestephanis/5a90f8a3919d50278a7e3257613be774 to your computer and use it in GitHub Desktop.
Save georgestephanis/5a90f8a3919d50278a7e3257613be774 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin name: Test Access-Control-Allow-Headers
*/
add_action( 'admin_menu', 'test_acah_admin_menu' );
function test_acah_admin_menu() {
add_management_page( 'Test Access-Control-Allow-Headers', 'Test ACAH', 'manage_options', 'test-acah', 'test_acah_admin_page' );
}
function test_acah_admin_page() {
?>
<div class="wrap">
<h1>Test!</h1>
<pre></pre>
</div>
<script>
(function($){
$.ajax( {
url: '<?php echo esc_js( rest_url() ); ?>wp/v2/users/1/',
method: 'GET',
beforeSend: function( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', '<?php echo esc_js( wp_create_nonce( 'wp_rest' ) ); ?>' );
}
} ).done( function( response ) {
console.log( response );
$( '.wrap > pre' )
.text( JSON.stringify( response, null, 2 ) )
.before( '<h3>User ID 1 details, fetched from the REST API:</h3>' +
'<p><em>(if it includes a `capabilities` key then it\'s an authenticated request)</em></p>' );
} );
})(jQuery);
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment