I have been busting my head trying to get the user ID with every piece of code possible but it always comes back "0". Not sure what I'm doing wrong. I started out using this:

<?php $user_info = get_userdata(1);
  echo 'Username: ' . $user_info->user_login . "\n";
  echo 'User level: ' . $user_info->user_level . "\n";
  echo 'User ID: ' . $user_info->ID . "\n";

Which kicks back a bunch of Function undefined errors which I take care of with "require_once" calling files such as pluggable.php, user.php etc... In the end, no more errors and the user is "0". Then tried this:

$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
  echo "Not logged in.";
} else {
  echo "logged in";

Which returns another function undefined: Fatal error: Call to undefined function wp_get_current_user() in .... Take care of that with the require_once list of files and I get "Not logged in" everytime. Then I went global with this:

<?php global $current_user;

      echo 'Username: ' . $current_user->user_login . "\n";
      echo 'User email: ' . $current_user->user_email . "\n";
      echo 'User first name: ' . $current_user->user_firstname . "\n";
      echo 'User last name: ' . $current_user->user_lastname . "\n";
     echo 'User display name: ' . $current_user->display_name . "\n";
     echo 'User ID: ' . $current_user->ID . "\n";

And I get this Fatal error: Call to undefined function get_currentuserinfo() in.... Putting the slew of require_once calls and I get this result:

Username: User email: User first name: User last name: User display name: User ID: 0

Not sure what I'm doing wrong. Out of desperation I even tried moving the file into my theme folder thinking that that might work but nothing.

Can anyone help me out??? Thanks in advance!

  • Where are you running this code out of? I presume not a theme or template file?
    – mor7ifer
    Commented Feb 5, 2012 at 21:14
  • 1
    You absolutely should not be requiring the pluggable.php file. The functions you are using are defined relatively late, which suggests that you are running your code way too early. Make sure you are hooking your functions to one of WordPress' action or filter hooks. @m0r7if3r's question is a good and the first step in getting this solved.
    – tollmanz
    Commented Feb 5, 2012 at 21:20
  • I'm running this out of a test file test.php at mirador.mx/oaxaca/test.php oaxaca being the root wordpress folder. I've already created a function script and tried various wordpress hooks such as muplugins_loaded, admin_bar_init and wp_loaded. I'm sure that I'm missing something really basic because I have limited programming knowledge. These hook attempts always give me a fatal or catchable error as well... usually function not defined. What hook do you suggest I use? And is my test.php page where it needs to be? Thanks soooooooo much for your input, both of you. @m0r7if3r
    – Arion
    Commented Feb 6, 2012 at 0:27
  • What is your end goal for the script? Is it going to be used in a template? A plugin?
    – mor7ifer
    Commented Feb 6, 2012 at 0:30
  • All I need is to pass the user ID to a flash swf file so the flash can make database changes for the current user. As far as I know, flash can't do this directly and one needs xml or php to get and direct the info where it needs to go. Sounded simple at first but turned into a nightmare shortly thereafter but what doesn't kill you makes you smarter, right? :) (That's if you ever figure it out.) Thanks for your input m0r7if3r.
    – Arion
    Commented Feb 6, 2012 at 1:48

1 Answer 1


To use wordpress functions outside the context of the wordpress environment, you can include wp-blog-header.php, so in the context of test.php:

  • Simon Scarfe... my hero! That's exactly what I needed. Thanks so much. And thanks to m0r7if3r for taking his time to answer as well. Is there anyway I can vote for you guys besides clicking on the "yes" button saying this post was helpful???
    – Arion
    Commented Feb 6, 2012 at 19:16
  • No probs. I believe the general etiquette on SE sites is to simply upvote helpful comments / answers, and accept the one that solves your problem. Commented Feb 6, 2012 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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