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 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:

<?php
$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;
      get_currentuserinfo();

      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!

share|improve this question
    
Where are you running this code out of? I presume not a theme or template file? –  m0r7if3r Feb 5 '12 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 Feb 5 '12 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 Feb 6 '12 at 0:27
    
What is your end goal for the script? Is it going to be used in a template? A plugin? –  m0r7if3r Feb 6 '12 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 Feb 6 '12 at 1:48
show 1 more comment

1 Answer

up vote 1 down vote accepted

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:

require('./wp-blog-header.php');
share|improve this answer
    
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 Feb 6 '12 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. –  Simon Scarfe Feb 6 '12 at 19:59
add comment

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.