No user found when using REST API

Question

When I use a REST route whilst logged in, and call wp_get_current_user(), no user is found. However, the request method, eg POST is processed without issue. Example REST route registration.

register_rest_route(
    'dewd-test-namespace', //namespace
    'dewd-test-namespace-test-route', //route
    [
     'methods'=>'POST',
     'callback'=>[
          $this,   //object of this class
          'testingUser' //function
      ] 
    ] //params
);

Call to wp_get_current_user() returns this user object:

WP_User Object
(
    [data] => stdClass Object
        (
        )

    [ID] => 0
    [caps] => Array
        (
        )

    [cap_key] => 
    [roles] => Array
        (
        )

    [allcaps] => Array
        (
        )

    [filter] => 
    [site_id:WP_User:private] => 0
)

i.e no user. If a permission callback is added e.g. `

'permission_callback' => function () {

     return current_user_can( 'edit_others_posts' );
 }

A 401 status is returned with the message “Sorry, you are not allowed to do that”.

I would appreciate it if anyone knows what I might need to do in order for wp_get_current_user() to return the current user in a REST call.

0
dewd 4 months 0 Answers 12 views 0

Leave an answer