簡體   English   中英

致命錯誤:未捕獲的錯誤:調用未定義的函數 wp_get_current_user()

[英]Fatal error: Uncaught Error: Call to undefined function wp_get_current_user()

我想將當前用戶 ID 存儲到數據庫中,但我得到了那個錯誤,php 文件是自定義的,我把它放在我的 wordpress 主題文件夾中,我該如何解決這個問題? 我想做一個簡單的自定義 php 腳本,可以將數據存儲到數據庫中。 而且我認為 wp_get_current_user() 是問題所在,我該如何正確使用它?

這是我的代碼

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$current_user = wp_get_current_user();

$current_user_id = $current_user->ID;

$title = mysqli_real_escape_string($conn, $_REQUEST['title']);
$department = mysqli_real_escape_string($conn, $_REQUEST['department']);
$date_today = date("Y-m-d");
$doc = $_FILES['fileName']['name'];
$doc_path = $_SERVER['DOCUMENT_ROOT'] . "/wordpress/wp-content/uploads/documents/" . $doc;



$sql = "INSERT INTO wp_document (ID,
                              department, 
                              title, 
                              submit_date,
                              doc_path,
                              revision_count,
                              rejection_count,
                              status) 
        VALUES ($current_user_id, '$department', '$title','$date_today', '$doc_path', 0, 0, 'Not Reviewed')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

如果您想使用 WordPress 功能或WPQuery

include('/var/www/html/pub_html/wp-blog-header.php');
$prefix =  $wpdb->base_prefix;

在您的自定義 PHP 中包含上述文件,它將建立 WordPress 連接

最近遇到同樣的錯誤並使用以下代碼修復

<?php
if(!function_exists('wp_get_current_user')) {
    include(ABS_PATH . "wp-includes/pluggable.php"); 
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM