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