Reputation: 443
I have a script running on the same server as my WP installation, and the user is logged in via WP.
I wish to check if the user is logged in, to prevent my script running for non logged in users, and access various user data.
When I try and access the data using:
wp_get_current_user()
I get an object with no user data.
My code looks like this
require('wp-blog-header.php');
$user = wp_get_current_user();
echo var_dump($user);
I expect the current logged in users data to be displayed.
Why is this the case.
I have tried using wp-login.php for the include, and I am sure I am logged in.
Upvotes: 0
Views: 520
Reputation: 356
Your code seems to work, I tried it on an external script too. So I believe the problem is the first line, if the required file is not in the same folder as your script.
Check if the script is in the same folder as the required wordpress and try to point to it. Example (as in the external script I tested):
require( __DIR__ . '/wp-load.php' );
$user = wp_get_current_user();
echo var_dump($user);
This works for me. You can also check wordpress functions like is_user_logged_in() or get_current_user_id(), which may serve you better in your purposes.
Upvotes: 1