Skip to content

Instantly share code, notes, and snippets.

@georgestephanis
Created February 3, 2016 23:58
Show Gist options
  • Save georgestephanis/0f12661dbbb5eac1cfcb to your computer and use it in GitHub Desktop.
Save georgestephanis/0f12661dbbb5eac1cfcb to your computer and use it in GitHub Desktop.
This is a tester for wp rest api authentication. You can call against unsecured and it'll always work, but change to secured and it'll only work for authed admins.
<?php
add_action( 'rest_api_init', 'myhacks_init' );
function myhacks_init() {
register_rest_route( 'myhacks', '/unsecured', array(
'methods' => 'GET',
'callback' => 'myhacks_response',
) );
register_rest_route( 'myhacks', '/secured', array(
'methods' => 'GET',
'callback' => 'myhacks_response',
'permission_callback' => 'myhacks_permission_callback',
) );
}
function myhacks_response() {
return array( 'doowop' => 'Data wants to be free, yo?' );
}
function myhacks_permission_callback() {
return current_user_can( 'manage_options' );
}
@serdarde
Copy link

Thank you! Worked well 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment