I'm trying to get logged in user id after if else statement but always return 0. When i put get_current_user_id() before it gives correct user id. How can I make it work in if statement?


function asd_endpt(){ 

//$user_id = get_current_user_id();
//echo $user_id; //<-- this returns user id

if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { //Get Real IP
} else {

if ($IP=="" || $IP=="") {
    $user_id = get_current_user_id();
    echo $user_id; //<-- this always return 0
  • What's the significance of those IPs? Is the issue that get_current_user_id() returns 0 inside an if statement, or that the user is not logged in when using those IPs? If you're connected with those IPs does WordPress otherwise show the user as being logged in? – Jacob Peattie Jan 30 at 3:35
  • Thanks for the reply @Jacob. Those IPs are from different servers wich sending a postback URL to my WordPress server (it's for security purposes). My code runs only when the user is logged in. Do you think I have to put something in if statement so get_current_user_id() can be accessed inside? – Lentis Jan 30 at 4:25
  • Why would there be a "current user" if they're postback requests? Did those servers 'log in' to the website? – Jacob Peattie Jan 30 at 4:31
  • Sorry, I'm new to this :( No, what I m trying to do is to credit my wp users when they completed some actions from those ips servers. – Lentis Jan 30 at 4:47
  • Well you won't be able to use get_current_user_id(), because those servers won't be logged in as that user. Those servers will need to send the user ID back to you, so that you can use that. If the postback includes ?user_id=1, for example, you would check $_GET['user_id'] instead. – Jacob Peattie Jan 30 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.