get_current_user_id()
即使在我登錄時也返回0
。我正在使用它在數據庫中添加一些值,但始終返回0
。get_current_user_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 {
...
}
}
沒有代碼沒有幫助! –
對不起,沒有足夠的信息繼續下去。我可以告訴你的是,你正在運行一個查詢... – Sablefoste
請提供一些你的代碼努力,所以至少有人可以建議進一步 –