0

I wanted to store the current user ID to the database, but instead I got that error, the php file is custom and i put it inside my wordpress theme folder, how can I fix this? I wanted to make a simple custom php script that can store data to the database. And I think wp_get_current_user() is the problem, how do I use it properly?

Here is my code

$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);
}
4
  • 1
    Check your includes Commented Nov 2, 2020 at 9:05
  • What do you mean? what should I check? I'm kinda new with wordpress Commented Nov 2, 2020 at 9:09
  • Does this answer your question? wordpress plugin -> Call to undefined function wp_get_current_user() Commented Nov 2, 2020 at 9:20
  • No, I still got an error "Fatal error: Uncaught Error: Call to undefined function add_action()" Commented Nov 3, 2020 at 3:01

2 Answers 2

1

If you want to use WordPress functions or WPQuery

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

Include above file on your custom PHP it will establish WordPress connections

Sign up to request clarification or add additional context in comments.

2 Comments

I just need to include wp-blog-header.php thanks for the help!! work like a charm!
This answer is incorrect. Not all WordPress located at the path you specified. Also, never use include(), use require() instead.
0

Recently Faced same error and fixed using below code

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

2 Comments

Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP) still got an error
Spelling mistake, it should be ABS_PATH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.