I have a custom WC API endpoint which I want to protect using WooCommerce authentication, ie:
my-site.com/wp-json/wc/v3/custom?consumer_key=ck...&consumer_secret=cs...
How would I achieve this? I know about JWT authentication but that's not acceptible in this case.
CodePudding user response:
add_action('rest_api_init', 'wc_custom_endpoint');
function wc_custom_endpoint(){
register_rest_route('wc/v3', 'custom', array(
'methods' => 'GET',
'callback' => 'return_value',
'permission_callback' => function($request){
return is_user_logged_in();
}
));
}
function return_value(){
return "this is my custom endpoint!";
}
Try this