0

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.

1 Answer 1

1

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.

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

2 Comments

I was making a silly mistake, my wp is https, and script was called over http. Once switched it all kicked in.
Thanks for this comment. That was exactly my case. Took me hours to stumble upon your comment and figure it out. Now both external app and WordPress are HTTPs and all works fine. Surprisingly when external app was HTTP and WordPress was HTTPs I could var_dump($_COOKIE) and see all data, but get_current_user_id() return zero.

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.