skip to Main Content

When trying to get the id of an authorized user wordpress gives 0.

phpmyadmin screen

<?php

$title = $_POST['title'];
$start = $_POST['start'];

try {
    require "db_config.php";
    require "wp-includes/user.php";
} catch(Exception $e) {
    exit('Unable to connect to database.');
}

$cur_user_id = get_current_user_id();

$sql = "INSERT INTO events (title, start, cur_user_id) VALUES (:title, :start, :cur_user_id)";
$q = $cal_db->prepare($sql);
$q->execute(array(':title'=>$title, ':start'=>$start, ':cur_user_id'=>$cur_user_id));

?>

2

Answers


  1. Chosen as BEST ANSWER

    Put my code in a new folder in plugins and activated in wordpress, everything worked. https://wp-kama.ru/id_2018/ajax-v-wordpress.html


  2. you can try this

    $cur_user_id = wp_get_current_user();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search