REST API: User not logged in
-
Hello,
I am trying to do rest calls via a plugin, but I keep getting the following error:
An error occurred: Sorry, you are not allowed to create posts as this user.When I debug, I notice that the get_current_user_id() is returning 0 in my function. Here is a snippet of the code I am trying to execute:
add_action( 'rest_api_init', function () { // Cart an item register_rest_route( 'screen/v1', '/cart', array( 'methods' => 'GET', 'callback' => 'screen_cart', ) ); } ); // Code to check the user id on init, which is returning the correct user ID //add_action('init', 'screen_getUserId'); //$screen_userId = 0; // //function screen_getUserId(){ // global $screen_userId; // $screen_userId = get_current_user_id(); // echo get_current_user_id(); //} function screen_cart($request) { // global $screen_userId; // However here it is returning 0 (meaning no one is logged in) return die(print_r(get_current_user_id(), true)); $cart = get_data_via_rest('/wp/v2/cart', 'POST', [ 'cart_user' => get_current_user_id(), ]); return $cart; }This is currently just a test app (prototype), is there a way I can make it so no authentication is required during development?
The topic ‘REST API: User not logged in’ is closed to new replies.