I'm trying to add the current user id from a Wordpress site to a table I have set up. All other values from inputs are going in fine, but I'm getting a php error when I try this code:
$link = mysqli_connect("localhost", "XXXXXX", "XXXXXX", "XXXXXX");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$latLng = mysqli_real_escape_string($link, $_POST['latLng']);
$title = mysqli_real_escape_string($link, $_POST['title']);
$type = mysqli_real_escape_string($link, $_POST['type']);
$current_user = wp_get_current_user();
$WP_id = $current_user->ID;
$sql = "INSERT INTO `user_locations` ( `id` , `WP_id` , `latLng` , `type` , `title` ) VALUES ('', '$WP_id', '$latLng', '$type', '$title')";
if(mysqli_query($link, $sql)){
header("Location: leaflet-test-page");
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
The error is:
Fatal error: Call to undefined function wp_get_current_user() in /home/XXXXX/public_html/XXXXXX/insert.php on line 17
Any help identifying the reason I am getting this error would be appreciated.