WordPress *get_current_user_id* returns 0 on remote server but not local server

Please I am working with WordPress and using Ajax to store some data into the database after i have retrieved the logged in user ID with wp function get_current_user_id() .

Everything works perfectly fine on my local server – localhost but on my remote server get_current_user_id() always return 0 instead of the logged-in user ID.

When i run the submit.php file directly get_current_user_id() returns the user ID fine

Please note that the submit.php and Ajax is not part of WordPress, it is in a seperate folder within the wordpress root folder

When i run the submit.php file seperately and test to see the resulting executing query the get_current_user_id() outputs the correct logged in user id and not 0



$connection = mysqli_connect("host", "user", "pass", 
"db"); // Establishing Connection with Server..
$db = mysqli_select_db($connection, "db"); // Selecting Database
//Fetching Values from URL
@$score = $_POST['score'];

$user_id = get_current_user_id();
//Insert query
$query = "update gamestats set bestscore = {$score} WHERE game='2048' AND 
playerid = {$user_id} and bestscore <= {$score}";
mysqli_query($connection, $query);

if (mysqli_affected_rows($connection) <= 0){
$ins_query = "INSERT INTO gamestats (bestscore, game, playerid) 
VALUES({$score}, '2048', {$user_id})";
mysqli_query($connection, $ins_query);
mysqli_close($connection); // Connection Closed

Ajax Call file is within same folder as the submit.php

        type: "POST",
        url: "submit.php",
        data: dataString,
        cache: false,
        success: function(result){
            //alert(score + " :Post Best Score");

The initiating code within WP template

<iframe height="1057" scrolling="no" align="middle" frameborder="0" src="http://www.xxxx.com/wp-folder/custom-folder/index.html">

The iframe is what initiate the whole process which i put with the WP Template.

It is within the index.html that the Ajax script is called

The “custom-folder” is my own custom folder placed within WordPress folder

NOTE: get_current_user_id() returns the correct user id on local server and everything works perfectly fine but on remote server it returns 0 storing 0 as the playerid into the DB all the time. What could be the problem please help?

Read more here: WordPress *get_current_user_id* returns 0 on remote server but not local server

Leave a Reply

Your email address will not be published. Required fields are marked *