Php get_current_user_id()即使在我登录时也返回0

Php get_current_user_id()即使在我登录时也返回0,php,wordpress,Php,Wordpress,get\u current\u user\u id()。我使用它在数据库中添加一些值,但它总是返回0 <?php include "class.Database.inc"; require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php'); define ("DIR_PATH", dirname(__FILE__)."/uploads"); define ("BOOK_FOLDER" ,"/book"); define ("COVER_FOLDER","/

get\u current\u user\u id()。我使用它在数据库中添加一些值,但它总是返回
0

<?php
include "class.Database.inc";
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
define ("DIR_PATH", dirname(__FILE__)."/uploads");
define ("BOOK_FOLDER" ,"/book");
define ("COVER_FOLDER","/cover");


function checkIfFileExistsAndReturnPath($location,$name,$ext){
    ...
}

function saveFile($file,$folder,$name)
{   
    ...

}

function uploadMedia($file, $post)
{
    ...
}

function returnJson ($message , $code)
{
        $msg = $message;
        $Code = $code;
        $array = array ('code' => $Code, 'message' => $msg); 
        header('Content-type: application/json'); 
        echo json_encode($array);
}

function authorExists ($id)
{
    if ($id == 0)
    {
        returnJson( "You are not logged in." , 6);
    }
    else {
    $db = Database::getInstance();
    $connection = $db->getConnection();

    $sql_query = "Select * from gp_posts where post_type = 'authorbook' AND post_author = '".$id."'";

    $result = $connection->query($sql_query);
    $number = 0;

    foreach ($result as $row)
    {
        $number++;
    }

    if ($number > 0)
    {
        return true;
    }

    return false;
    }
}

function getAuthorId ($id)
{
    $db = Database::getInstance();
    $connection = $db->getConnection();

    $sql_query = "Select ID from gp_posts where post_type = 'authorbook' AND post_author = '".$id."'";

    $result = $connection->query($sql_query);
    $author_post_id = NULL;
    foreach ($result as $row)
    {
        $author_post_id = $row['ID'];
    }

    if ($author_post_id != NULL)
    {
        return $author_post_id;
    }

    return NULL;
}

function createAuthor ($id)
{
    $current_user = wp_get_current_user();

    $my_post = array(
            'post_title'    => $current_user->user_firstname." ".$current_user->user_lastname ,
            'post_status'   => 'publish',
            'post_author'   => $user_id,
            'post_type' => 'authorbook',
            'comment_status' => 'closed'
            );


            // Insert the post into the database
            $post_id = wp_insert_post( $my_post , $wp_error );

            if ($post_id)
            {
                 return $post_id;
            }
}


$code = 0;
function cleanPostData($post_data){
    $post_data = trim($post_data);
    $post_data = stripslashes($post_data);
    $post_data = htmlspecialchars($post_data);
    return $post_data;
}

if ($_SERVER["REQUEST_METHOD"] == "POST"){

    $title = stripslashes(htmlspecialchars($_POST['title']));

    $genre = cleanPostData($_POST['genre']);

    $language = cleanPostData($_POST['language']);

    $book_file = $_FILES['book'];
    $book = $_FILES['book']['tmp_name'];
    $book_file_name = $_FILES['book']['name'];

    $book_description = cleanPostData($_POST['book_description']); 

    $book_front_cover_file = $_FILES['book_front_cover'];
    $book_front_cover_name = $_FILES['book_front_cover']['name'];
    $book_front_cover = $_FILES['book_front_cover']['tmp_name'];
    $book_front_cover_id = "";

    $book_back_cover_file = $_FILES['book_back_cover'];
    $book_back_cover_name = $_FILES['book_back_cover']['name'];
    $book_back_cover = $_FILES['book_back_cover']['tmp_name'];
    $book_back_cover_id = "";

    $user = wp_get_current_user();
    $user_id = $user->ID;
    //$user_id = get_current_user_id(); 
    $author = authorExists($user_id) ? getAuthorId($user_id) : createAuthor($user_id);



    //echo var_export($book_file_name);echo "<br>";

    if (empty($title))
    {
        returnJson("Please provide the title of your book." , 1);
    }

    else if (empty($genre))
    {   
        returnJson("Please select a genre.", 2);
    }

    else if (empty($book_file_name))
    {   
        returnJson("Please select a book." , 3);
    }

    else if (empty($book_description))
    {   
        returnJson("Please provide a sdescription about the book." , 4);
    }


    /*else if (empty($author))
    {
        returnJson("Please select an author, if your desired author is not in the list you can create a new author from above." , 5);

    }*/

    else {
        ...

    }

}

尝试在
init
操作中添加
get\u current\u user\u id()
函数以获取当前用户id

像这样:

add_action('init', 'myFunction');

function myFunction(){
   $user_ID= get_current_user_id();     
   // THEN DO YOUR CODE OR QUERY TO ADD VALUE IN DB.
}

希望这将有助于和工程为您。谢谢。

如果您尝试REST api请求,默认情况下该请求未经身份验证,并且用户ID自动设置为0,请参阅REST api:

对于手动发出Ajax请求的开发人员,每个请求都需要传递nonce。API使用nonce,动作设置为wp_rest。然后可以通过_wpnoce数据参数(POST数据或GET请求查询中)或通过X-WP-Nonce头将这些数据传递给API。如果未提供nonce,API会将当前用户设置为0,将请求转换为未经验证的请求,即使您登录到WordPress

您只需在请求中添加一个nonce即可解决此问题

在AJAX标题中

$.ajax({
  url: '/wp-json/my-plugin/v1/uid',
  method: 'GET',
  beforeSend: function(xhr) {
    xhr.setRequestHeader('X-WP-Nonce', settings.nonce);
  }
})
或者在表单中使用隐藏字段

$_wpnonce = wp_create_nonce( 'wp_rest' );
echo "<input type = 'hidden' name = '_wpnonce' value = '$_wpnonce' />";
$\u wpnice=wp\u create\u nonce('wp\u rest');
回声“;

无代码无帮助!对不起,没有足够的信息继续下去。我所能告诉你的是,你正在某处运行一个查询…请提供一些你的代码,这样至少有人可以建议进一步显示你的部分代码,你是如何使用这个功能的请检查,我已经上传了代码。