2

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:

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
    }
5
  • why do you have a PHP file where you need to do this? Generally this isn't a good idea. Give us an idea of what you're trying to accomplish and maybe we can give you alternatives. Commented Nov 6, 2011 at 22:24
  • 1
    In my case I need to do this. The files is actually inside my theme folder so it's inside wordpress but not part of it. I need this because my image uploader submits to this file where I need to specify where the image will more to and I need to move it to a folder named the same as the user login so I need to know the current user login name. This is why I need to get wordpress's current logged in user details. Commented Nov 6, 2011 at 22:36
  • 1
    You don't need to do this... Whatever code you have in that .php file can easily be moved to the theme's functions.php file, in which you already have access to the current user info... Commented Nov 6, 2011 at 23:07
  • 1
    Is there a way of doing it this way though? Commented Nov 6, 2011 at 23:08
  • FWIW I'm not sure why the original poster had issues... I was able to include wp-load.php and get the user info just fine. See Sagive SEO's answer below. Commented Oct 18, 2012 at 1:37

3 Answers 3

4

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.

10
  • OK, I've tried the code and it didn't work. Nothing is returned. Commented Nov 7, 2011 at 0:57
  • Sorry.. My Bad.. Check the fixes Commented Nov 7, 2011 at 1:32
  • OK, there must be something missing? I'm getting this error: <b>Fatal error</b>: Call to undefined function is_user_logged_in() in <b>/home/mydomain/public_html/wp-content/themes/mytheme/uploadify.php</b> on line <b>25</b><br /> Commented Nov 7, 2011 at 19:16
  • what is that page uploadify.php? a page temaplte? can you paste its code in pastebin and link here? Commented Nov 7, 2011 at 19:37
  • 1
    I think wp-load.php is enough, no need for the header, I don't think, which includes template-loader.php as well. Commented Jan 30, 2012 at 4:05
0

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 );
0

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] => [email protected]
            [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
)

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.