I have created a file in some directory outside wp-content
where I load the default WordPress file to run the following code. Lets say the file can be accessed using domain.com/folder/user_info.php
.
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once(__DIR__."/../wp-load.php");
require_once(__DIR__."/../wp-config.php");
$connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
var_dump(wp_get_current_user());
When file is placed here, the output is:
object(WP_User)#2555 (8) { ["data"]=> object(stdClass)#3303 (0) { } ["ID"]=> int(0) ["caps"]=> array(0) { } ["cap_key"]=> NULL ["roles"]=> array(0) { } ["allcaps"]=> array(0) { } ["filter"]=> NULL ["site_id":"WP_User":private]=> int(0) }
When I place the same file inside wp-content
and access it using domain.com/wp-content/user_info.php
, I can get the information about the current user just fine.
The problem is that I don't want to put my own files inside wp-content
. So, is there any way to get the information of logged in user outside the wp-content
directory?
Thanks.
UPDATE:
This is my directory structure:
domain/
wp-content/
user_info.php
wp-includes/
wp-admin/
custom-folder/
user_info.php
The code is exactly the same in both user_info
files. However, the file inside wp-content
prints the user information but the file inside custom-folder
does not.
I test your code, and it's working fine on my end.
Try the alternate:
ini_set('display_errors', 1);
require_once(__DIR__."/../wp-load.php");
require_once(__DIR__."/../wp-config.php");
$connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
global $current_user;
var_dump($current_user);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.