user meta - wp_get_current_user in custom file returns 0

admin2025-01-07  6

I need to access logged-in user data in a custom PHP file that I call in a WP page through an include.

The include file is:

require($_SERVER['DOCUMENT_ROOT'].'/wp-config.php'); 
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php'); 
$current_user = wp_get_current_user(); 
var_dump($current_user->ID);
var_dump($current_user->display_name);

And it outputs: int(0) bool(false) int(0) bool(false)

However... In the very same page, I also call a function that I entered in function.php:

$current_user = wp_get_current_user();
var_dump($current_user->ID);
var_dump($current_user->display_name);

And this one outputs: int(1) string(10) "antoine251"

It looks to me like WP functions called from my external php file are not working.

What am I missing? (No pluggin is even installed, same results in twentynineteen theme than is my custom theme)

Thanks in advance for your help!

I need to access logged-in user data in a custom PHP file that I call in a WP page through an include.

The include file is:

require($_SERVER['DOCUMENT_ROOT'].'/wp-config.php'); 
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php'); 
$current_user = wp_get_current_user(); 
var_dump($current_user->ID);
var_dump($current_user->display_name);

And it outputs: int(0) bool(false) int(0) bool(false)

However... In the very same page, I also call a function that I entered in function.php:

$current_user = wp_get_current_user();
var_dump($current_user->ID);
var_dump($current_user->display_name);

And this one outputs: int(1) string(10) "antoine251"

It looks to me like WP functions called from my external php file are not working.

What am I missing? (No pluggin is even installed, same results in twentynineteen theme than is my custom theme)

Thanks in advance for your help!

Share Improve this question edited Jan 27, 2019 at 15:35 AnG251 asked Jan 27, 2019 at 15:28 AnG251AnG251 313 bronze badges 2
  • 1 You try to get user data to early – Krzysiek Dróżdż Commented Jan 27, 2019 at 15:37
  • Thanks Krzysiek, what I am I missing prior to getting user data? Sorry for my questions, but I'm totally lost! – AnG251 Commented Jan 27, 2019 at 15:41
Add a comment  | 

2 Answers 2

Reset to default 0

The problem with your code is that you want to get current user to early.

wp_get_current_user is using global current_user variable.

But this variable isn’t set from the beginning.

And if you want to use this function straight in the script file, then it’s to early.

You should wait for init action to get current user.

require($_SERVER['DOCUMENT_ROOT'].'/wp-config.php'); 
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php'); 

add_action( 'init', function() {
    $current_user = wp_get_current_user(); 
    var_dump($current_user->ID);
    var_dump($current_user->display_name);
});

try this code

wp has finished initializing before executing your custom function.

require($_SERVER['DOCUMENT_ROOT'].'/wp-config.php'); 
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php'); 

// Wait until WordPress is fully initialized
add_action('wp_loaded', 'my_custom_function');

function my_custom_function() {
    $current_user = wp_get_current_user(); 
    var_dump($current_user->ID);
    var_dump($current_user->display_name);
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253790a152.html

最新回复(0)