Get current user data from external PHP page

I have a PHP page at the same level as the template/theme on WordPress.
I need to be able to get the current logged in user details from this page.

I have tried this:

Read More
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

global $current_user;
$current_user = wp_get_current_user();
var_dump( $current_user );

But it’s returning nothing. 0 as user_id and nothing on the other fields. Am I missing something?

UPDATE:

This is the result of the var_dump:

object Object]1object(WP_User)#79 (10) {
      ["data"]       => NULL
      ["ID"]         => int(0)
      ["id"]         => int(0)
      ["caps"]       => array(0) {}
      ["cap_key"]    => NULL
      ["roles"]      => array(0) {}
      ["allcaps"]    => array(0) {}
      ["first_name"] => string(0) ""
      ["last_name"]  => string(0) ""
      ["filter"]     => NULL
    }

Related posts

Leave a Reply

3 comments

  1. You can…

    Load the file into the file where you want to display the ‘hey username’ message:

    <?php include(TEMPLATEPATH .'/check-user-hello.php'); ?>
    

    .
    Then in that file “check-user-hello.php”
    You need to put this code

    <?php
    if ( is_user_logged_in() ) {
    global $current_user;
    get_currentuserinfo();
    echo 'Hey ' . $current_user->display_name;
    } else {
    echo '<a href="'. get_bloginfo('url') .'/wp-admin" class="loginlinktop">Login</a>';
    }
    ?>
    

    .
    Hope This Helps 🙂

    TO learn more about this subject:

    .
    FIX 3

    To the best of my knowledge you need to grab wp-blog-header.php in order to run WordPress functions outside of the loop.. so.. try this.

    <?php
    
    require('../../../wp-blog-header.php');
    
    if ( is_user_logged_in() ) {
    global $current_user;
    get_currentuserinfo();
    echo 'Hey ' . $current_user->display_name;
    } else {
    echo '<a href="'. get_bloginfo('url') .'/wp-admin" class="loginlinktop">Login</a>';
    }
    ?>
    

    .
    Please NOTE:
    THE “wp header blog” PATH i have created in this code is assuming your file is in your template directory.. if its not you should change the path of require so it would load the file correctly.

  2. I kept running into an issue until I realized that I had copied some extra code from the snippet I found on a search.

    define( 'SHORTINIT', true );
    

    When I replaced ‘true’ with ‘false,’ it worked like a charm.

    define( 'SHORTINIT', false );
    
  3. Hello late blooming stumblers (like me). As I have been struggling with this problem, and answers that indicate require wp-load.php or wp-blog-header.php should work, if they ever worked, do not work with later releases, the solution seems to be to load wp-load.php AND THEN invoke the wp_head() function. So, without further ado,

    require('../../../../../wp-load.php'); //Obviously my own crude construction of the path the wp-load.php file.
    wp_head();
    
    $user = get_user_by('ID',57); //gets user object by ID. Change '57' to some valid ID in your own installation.
    
    echo "<pre>";
    print_r($user);
    echo "</pre>";
    

    …and Bob’s your uncle.

    Outputs:

    WP_User Object
    (
        [data] => stdClass Object
            (
                [ID] => 57
                [user_login] => unclebob
                [user_pass] => $P$BkVVl8RhOc/niffq1QaNyaMeNwvpoO0
                [user_nicename] => unclebob
                [user_email] => uncle.bob@unclebob.online
                [user_url] => 
                [user_registered] => 2019-11-29 08:38:45
                [user_activation_key] => 1575016726:$P$BE/a1mnbiD4unzrYG5O0L1T1pJZ5GG.
                [user_status] => 0
                [display_name] => Uncle Bob, PhD
            )
    
        [ID] => 57
        [caps] => Array
            (
                [admin] => 1
            )
    
        [cap_key] => wp_capabilities
        [roles] => Array
            (
                [0] => admin
            )
    
        [allcaps] => Array
            (
                [0] => read
                [admin] => 1
            )
    
        [filter] => 
        [site_id:WP_User:private] => 1
    )