Skip to content

Instantly share code, notes, and snippets.

@pinalj
Last active December 1, 2017 06:49
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 pinalj/cb9f72bcb6a6e9867b067fc7575ac8ac to your computer and use it in GitHub Desktop.
Save pinalj/cb9f72bcb6a6e9867b067fc7575ac8ac to your computer and use it in GitHub Desktop.
Check for permissions for API
<?php
add_action( 'rest_api_init', 'my_register_route' );
function my_register_route() {
register_rest_route( 'my-route', 'my-phrase', array(
'methods' => 'GET',
'callback' => 'custom_phrase',
'permission_callback' => function() {
return current_user_can( 'edit_posts' );
},
)
);
}
function custom_phrase() {
return rest_ensure_response( 'Hello World! This is my first REST API' );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment