When to return 0 from get user ID?

When to return 0 from get user ID?

At the tail end, the return value is return ( isset ( $user->ID ) ? (int) $user->ID : 0 ); — this code will return 0 if the logged-in user is somehow unavailable. Use the init or any subsequent action to call this function. Calling it outside of an action can lead to troubles.

How to get CURRENT USER ID in WordPress?

Going by wp_get_current_user () information in the Codex, the function utilizes the global $current_user object and if necessary initializes it before use. As others have stated, get_current_user_id () uses this function on the backend.

Why do I not get CURRENT USER ID in PHP?

However, if you just randomly grab for that user information before init action is called, you will not get a current user. This explains why you got the User’s ID when adding those actions into functions.php (as those actions take place after init ), whereas with your original code it’s not clear when you invoke that.

Is there a way to get the user ID?

Dmitry Sikor… If it returns false, you can’t get the user id, because there is no user. Try to sign in user first, then you will be able to get user id. Hi Dmitry, my result variable is always returning succeed.

Is there a way to enable crossdomain calls on client side?

There is nothing you can do on your end (client side). You can not enable crossDomain calls yourself, the source (dailymotion.com) needs to have CORS enabled for this to work. The only thing you can really do is to create a server side proxy script which does this for you.

Why is my jQuery Ajax request being blocked?

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.dailymotion.com/embed/video/x28j5hv . (Reason: CORS request failed).

Why does fetch return a response with status = 0?

Why status is 0 and how can I get the correct res like the one in the chrome network ? no-cors — Prevents the method from being anything other than HEAD, GET or POST. If any ServiceWorkers intercept these requests, they may not add or override any headers except for these.

Why does PHP get user ID return 0?

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.

How to get current logged in user in PHP?

First you authenticate the client sending a HTTP POST request to the endpoint /wp-json/jwt-auth/v1/token sending username and password fields to generate a auth token. Then you pass the token each request settings the request header Authorization like: I was dealing with the same issue where get_current_user_id () would return 0.