wp_get_current_user() function not working under custom endpoint.
-
I cannot get wp_get_current_user() on my custom WordPress endpoint.
function spinwrite_endpoint() { register_rest_route('spinwrite', '/rewriter/', Array( 'methods' => 'GET', 'callback' => Array($this, 'rewriter') )); } function rewriter( $request ) { return wp_get_current_user(); }
When I go to mywebsite.com/wp-json/spinwrite/rewriter when I am logged in, it gives me a response as though I’m not logged in.
{"data":{},"ID":0,"caps":[],"cap_key":null,"roles":[],"allcaps":[],"filter":null}
Also, I’ve tried other functions that access the user such as . . .
is_user_in_role('edit_posts');
. . . and WordPress treats me as though I’m logged out and have no permissions.This is really important to get working because I can’t secure my endpoint without some form of checking who is accessing it.
I did try installing the WP REST API v2 plugin, but then my endpoint disappeared and when I uninstalled the WP REST API v2 plugin my endpoint was still gone. I had to completely reinstall WordPress just to get it to work again.
I don’t want to use an external plugin for my endpoint authentication if I can help it anyways because then my plugin is dependant on it.
- The topic ‘wp_get_current_user() function not working under custom endpoint.’ is closed to new replies.