Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a php page at the same level as the template / theme on wordpress.

What I need it 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.

I'm 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 }

share|improve this question
    
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. –  chrisguitarguy Nov 6 '11 at 22:24
    
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. –  user159500 Nov 6 '11 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... –  onetrickpony Nov 6 '11 at 23:07
    
Is there a way of doing it this way though? –  user159500 Nov 6 '11 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. –  Ryan Oct 18 '12 at 1:37
add comment

1 Answer

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.

share|improve this answer
    
OK, I've tried the code and it didn't work. Nothing is returned. –  user159500 Nov 7 '11 at 0:57
    
Sorry.. My Bad.. Check the fixes –  Sagive SEO Nov 7 '11 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 /> –  user159500 Nov 7 '11 at 19:16
    
what is that page uploadify.php? a page temaplte? can you paste its code in pastebin and link here? –  Sagive SEO Nov 7 '11 at 19:37
    
Here is the code for the file uploadify.php: –  user159500 Nov 7 '11 at 21:05
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.