• Resolved guardiano78

    (@guardiano78)


    Hello,
    i need to read the id of the user logged in through ajax.
    the php file (user.php) that I call from the ajax script contains the following code:

    
    define('WP_USE_THEMES', false);
    require('../wp-load.php');
    
    $user_id = get_current_user_id();
    echo $user_id;
    

    If I run this page directly from the browser, I get the correct id of the logged in user running the page.
    If instead I call the php file from the Ajax script, I get the user id equal to 0.

    How can I solve the problem?
    thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • I would recommend you using wp-admin/admin-ajax.php rather than your own php file and the appropriate hooks to a function.

    This way you are less likely to run into these sort of issues

    see https://developer.www.ads-software.com/plugins/javascript/ajax/

    Thread Starter guardiano78

    (@guardiano78)

    Hello Alan,
    thank you for reply.
    The code that I posted is only for example.
    To tell the truth, after reading the id of the logged in user, the php script must make a query on a table external to wordpress and then download a certain file. I’ve never used “admin-ajax.php”, I should read up … I don’t know if it’s the best solution in this specific case. However, once the logged-in user reading problem has been solved, the rest of the work has already been completed.
    Thank you.

    Thread Starter guardiano78

    (@guardiano78)

    Hello,
    I found that you need to make some changes on the server side.
    The code I posted works fine, so I close the post it’s fixed.
    Thank you.

    Great you got it sorted.

    Worth looking at using admin-ajax.php is the (older) ‘WordPress’ way. It is ‘neat’ in that you can contain everything inside a little ‘plugin’ and then you are in the wordpress structures.

    However ..

    If you want to get even slicker, you can create a custom REST API end point to do what you want to do. This is the approach I would take for your particular use case. So when you are bored and want to refactor your code ?? this is the one I’d bee looking at if I were you https://developer.www.ads-software.com/rest-api/extending-the-rest-api/adding-custom-endpoints/

    But as your code is working – then that solves your issue.

    Moderator bcworkz

    (@bcworkz)

    FWIW – Requiring wp-load.php is discouraged because it’s not portable to any random WP installation. We cannot rely on relative references to work on all sites because some WP folders can be relocated or renamed. By localizing the path to admin-ajax.php with admin_url() as shown in Alan’s Plugin Handbook link we avoid such issues.

    That said, requiring wp-load.php should not cause issues when setup to work for your own specific site.

    • This reply was modified 3 years, 4 months ago by bcworkz.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to get user id logged in’ is closed to new replies.