0

I need to get the current user id to lookup customer information, in order to pass it to an api call to process a payment. I have tried:

$user = wp_get_current_user();
echo ( isset( $user->ID ) ? (int) $user->ID : 0 );

and:

$userid = get_current_user_id();
echo $userid;

But when I run the php file, the process stops at the first line and does not display anything after it. I dont get an error or nothing, just a blank screen. I have been searching for solution for 2 weeks. All results says that these codes should work, but does not for me. I have also added the require('to user.php') and doe not work.

I even enter this php code into Dreamweaver, and "wp_get_current_user" or "get_current_user_id" does not turn blue like other function calls do, but get_current_user() does turn blue, but this is the websites owners user not the current user. Please help if you can, much appreciated. Thanks Here is my code:

<?php 

echo "here";

/**
 * Lookup the customer information.
 *
 * @since 1.0.0
 *
 * @return void
 */
 echo "is";
function lookup_customer_information() {
    $current_user = wp_get_current_user();
    if ( ! is_object( $current_user ) || ! isset( $current_user->ID ) ) {
        echo $current_user;
        return;
    }
echo "the";
echo "code";
    // do your business logic here.
}
     ?>

<?php $userinfo = lookup_customer_information();
  echo $userinfo;
?>
sharon
  • 1
  • 1
  • 1
    Where did you add this PHP code, in your template files? It sounds like Wordpress isn't loaded completely yet. – giraff Dec 02 '16 at 18:57
  • 3
    A blank screen is indicative of an overall error. Check your error logs and/or [turn on error reporting](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – castis Dec 02 '16 at 19:00
  • I created a new test.php file and added the code there. How do I make sure that wordpress has already loaded? – sharon Dec 02 '16 at 19:46

1 Answers1

1

From what you describe, it sounds like WordPress Core has not loaded the wp-includes/user.php file yet or the current user has not been setup yet.

WordPress, like all apps, load in a particular order. What may be causing the issue for you is the current user has not been setup and/or the user files (in Core) have not loaded yet.

Let's See if Function is Available

Let's discover if the function wp_get_current_user is ready to be used. You can do any of these:

  • Check the PHP error logs
  • Check your settings to display the errors
  • Put code in to check for it

Let's use the third option to show you if it's available or not. Put this code snippet in above your if conditional you show in your question:

if ( ! function_exists( 'wp_get_current_user' ) ) {
    die('The file with wp_get_current_user() has not loaded yet into memory.');
}

The code will use the PHP construct function_exists to check if wp_get_current_user has been loaded into memory. If yes, you will not get the message, as it's ready. Else, the website will stop loading and you'll get the above message displayed on the screen.

No, It's Not Available

If the website renders the above message on the screen, then you know it's not loaded into memory yet. That means you are trying to use it before WordPress Core has loaded it.

How to Fix

Delay. You want to wait until WordPress has setup the user. Typically, you can register to the init event or later. For example, you can do this:

add_action( 'init', 'lookup_customer_information' );
/**
 * Lookup the customer information.
 *
 * @since 1.0.0
 *
 * @return void
 */
function lookup_customer_information() {
    $current_user = wp_get_current_user();
    if ( ! is_object( $current_user ) || ! isset( $current_user->ID ) ) {
        // you might want to post a message
        return;
    }

    // do your business logic here.
}

Yes, It Is Available

When running the function_exists() snippet above, you did not get the message. Hum, something else is up then.

Now, it's time to dig into your PHP error logs to see what is going on. You can post them to help us help you.

Community
  • 1
  • 1
hellofromTonya
  • 1,301
  • 8
  • 8
  • thank you for that information. I used the if ( ! function_exists( 'wp_get_current_user' ) ) { die('The file with wp_get_current_user() has not loaded yet into memory.'); } code and the file was not loaded. I had the require_once('user.php') so it could find the function get_current_user_id(), so I added require_once('pluggable.php') then it would find wp_get_current_user() but then could not find get_current_user_id(); I tried the other lookup_customer_information code you supplied, but that would not run either. I cleared out the error log , but it is still empty. – sharon Dec 03 '16 at 02:08
  • You should NOT have to include any WordPress Core files. Why? They load automatically. You are having to load them manually because you are running your code too early. WordPress has not loaded yet. Use the code I showed you above, where you register to the `init` event. Then you will know WordPress is loaded. – hellofromTonya Dec 03 '16 at 02:59
  • I removed all the code and just added your two sets of codes. the first one response with wp_get_current_user has not loaded. and that is all the further it goes. I have added this page as a subpage to the website, so I know that wordpress has loaded. then I reversed the two sets of codes, and it does nothing as soon as it gets to the init call. – sharon Dec 03 '16 at 03:51
  • Let's recap. You put in the "How to Fix" code, which wraps your code into the function `lookup_customer_information()` and the white screen still appears. Is this correct? – hellofromTonya Dec 03 '16 at 03:58
  • Please share your updated code so that we can take a look. You can update your original question with a new section. – hellofromTonya Dec 03 '16 at 04:07
  • do you want me to create a new question? this the first time I have asked questions on here. – sharon Dec 03 '16 at 04:16
  • Nope, you can amend your original question. Just add a new section at the bottom with the updates. – hellofromTonya Dec 03 '16 at 04:17
  • Look at your code and compare it again to the code I gave you in the How to Fix section. You have to call the function as a callback to the event "init". Why? Why can't you call it directly as you have? WordPress hasn't loaded completely yet. When it fires the event "init", it call call your function and run the code inside of it. – hellofromTonya Dec 03 '16 at 04:34
  • I added the init code after the first echo "here"; and that is all I get is "here". – sharon Dec 03 '16 at 04:40
  • Make sure you've removed the last 2 lines of code, where you are setting the variable $useinfo and then echoing. All of your code should inside of the function, except for the add_action(). – hellofromTonya Dec 03 '16 at 05:03
  • I did already remove it. I think I might know what the issue is. I am calling this php file from one of the pages, but I have it inside an iframe. Do you think that is why the function calls are not working? Any other ideas on how to call a php file from a wordpress page? – sharon Dec 03 '16 at 05:05
  • Okay, @sharon, you need to do some research now to learn how to build a plugin or theme. Your code should be within either of these. To start learning, you can work with one of the default themes. Then add your custom file to it. Next, you need to load your file. For now, you can do an `include()` in the functions.php file. That file is the bootstrapping launch point of a theme. – hellofromTonya Dec 03 '16 at 05:09