How to get current logged in user using Wordpress Rest Api?

后端 未结 5 1910
盖世英雄少女心
盖世英雄少女心 2020-12-25 11:32

I tried to add a custom request.

add_action(\'rest_api_init\', function () {
    register_rest_route( \'custom\', \'/login\', array(
        \'methods\' =>         


        
5条回答
  •  一生所求
    2020-12-25 12:02

    I was dealing with the same issue where get_current_user_id() would return 0. After some testing I found this was only the case when sending POST requests.

    With GET requests the function returned the logged in user ID, on a POST request it returned 0. This could be due to a server configuration perhaps as this was on a shared hosting environment. I did not find any posts or comments mentioning this before, but this is what happened in my case. A simple workaround could be to pass the user ID back to the frontend in a GET request, store it and send it on POST requests as an extra param.

提交回复
热议问题