Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save justingreerbbi/cee7b2c5dd8b415c473d99f0c58d1e82 to your computer and use it in GitHub Desktop.

Select an option

Save justingreerbbi/cee7b2c5dd8b415c473d99f0c58d1e82 to your computer and use it in GitHub Desktop.
WP REST API OAUTH2 - WP OAUTH SERVER EXAMPLE - AUTHENTICATION
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
'permission_callback' => function () {
return current_user_can( 'manage_options' );
}
) );
} );
function my_awesome_func() {
$user_id = get_current_user_id();
return array(
'status' => true,
'message' => 'Congrats! You successfully made an authenticated request to a protected endpoint',
'user_id' => $user_id
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment