3

I'm trying to figure out how to use the function get_current_user_id() properly.

I need it for user data separation just like a normal PHP code would use $_SESSION.

I've found the following code sample which I've placed in Function.php and it works fine, but it seems to be executed on every page which is not the intention.

function hf_Function(){

    $user_ID = get_current_user_id(); 

    _SESSION["uid"] = user_ID;
}
add_action('init', 'hf_Function');

I just need it to execute once and save the information in a $_SESSION variable or a global variable that I can access from my own PHP pages.

I tried to put the lines above in my own PHP script but it doesn't seem to work.

Is this the right way to do it or am I doing something wrong?

What is best practice when it comes to using get_current_user_id() and the other built-in functions?

1
  • Okay thanks :o) 1) & 2) are type errors. 3) I actually did try to read the description on add_action (init) but I was still not sure how to do it the right way. But my final goal is to use the User ID for my SQL when selecting data så that user data wont get mixed up. In the PHP I've done until now (not WP) I used $_SESSION variables for user separation. Mar 1, 2016 at 21:55

1 Answer 1

5

Based on your example code, there is no benefit to you storing the user_id in the $_SESSION.

You can use the get_current_user_id() method throughout the site. It will return the current users ID if they are logged in, or it will return 0 if the current user is not logged in.

For example, you could do the following:

function hf_Function(){
    $user_ID = get_current_user_id(); 

    if ($user_ID == 0) {
        // The user ID is 0, therefore the current user is not logged in
        return; // escape this function, without making any changes
    }

    // Run your SQL queries here (using WordPress DB class)
    global $wpdb;
    // $wpdb->query($sql);
}
add_action('init', 'hf_Function');

Because you're using the init action, this function will run on every page load. You may need to change the action, depending on what you're trying to achieve. This answer on WordPress SE might be useful to help you understand the WordPress lifecycle.

3
  • I need to Read some more abort hooks in WP and how they work. One additional question that has come into my mind though is the placement of the code. It seems that When I place the code in my own PHP file it doesn't work but When I place it in the Functions.php file it works fine. Am I missing something...... Do I need to registre my own PHP file somewhere ??? Mar 5, 2016 at 8:47
  • @FlemmingLemche WordPress will automatically include your functions.php file when it first loads. If you create separate file, i.e. my-functions.php then WordPress has no knowledge of that file. You would need to include that file in your functions.php file: include('my-functions.php'); in order for WordPress to recognise and process any code contained within.
    – Kirk Beard
    Mar 5, 2016 at 9:34
  • 1
    You are the man. Thank you so much for helping out. One thing is to know PHP another is to learn to operate in the WP framework. As I said ealier I'v just opened Pandoras box and are at the noop level. 😎 Mar 6, 2016 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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