I need to add some data to the user's meta on my website. To do so, I use ajax to call a PHP file on my server, however, when the code is executed, it just returns null instead of the user class. Here is the JS code:
<script>
jQuery.ajax({
type: "POST",
url: 'https://mywebsite.com/wp-content/ajax/file.php',
dataType: 'json',
data: {},
success: function (obj, textstatus) {
console.log(obj);
}
});
</script>
And the contents of file.php are:
<?php
header('Content-Type: application/json');
global $current_user;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// 3 llines above are just to display the error if any
get_currentuserinfo();
echo json_encode($current_user->ID);
?>
When I execute this, the error says that Uncaught Error: Call to undefined function get_currentuserinfo() in...
When I delete this line, the output is just null. I don't know If I'm doing everything correctly, but feel free to suggest different solutions. I do not need the ID to be returned, but the $current_user variable should not be empty.