Site icon Hip-Hop Website Design and Development

Change wordpress’ database data using ajax – how to get current user id?

I want to update a wordpress’ database using ajax. I’ve created php file and included both

wp-includes/user.php’

wp-includes/pluggable.php’

and when I call

$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;

it says that the function wp_get_current_user() is undefined.

PHP code:



include('../../wp-includes/user.php');
include('../../wp-includes/pluggable.php');

$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;

$number = $_POST['number']; 

$query = "UPDATE wp_users SET number = number - 500 WHERE id = '$current_user_id'";

$dbhost = "changed";
$dbuser = "changed";
$dbpass = "changed";
$dbname = "changed";

$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

if(! $conn ){
   die('Could not connect: ' . mysqli_error($conn));
}


$retval = mysqli_query($conn, $query);
if(! $retval ) {
   die('Could not update data: ' . mysqli_error($conn));
}
echo "Updated data successfullyn";
mysqli_close($conn);
?>