• I want to fetch logged in user data in an external PHP file. The file contains PHP code that display records from MySQL table to frontend page.

    When I try to include the wp-load.php file in my PHP file. PHP code stop showing data on the front page.

    I am using this code to include wp-load.php file

    require_once(rtrim($_SERVER[‘DOCUMENT_ROOT’], ‘/’) . ‘/wp-load.php’);

    $current_user = wp_get_current_user();
    $username=$current_user->user_login;

    My custom PHP file path is /api/advance.php

    Looking for help from experts to solve this issue.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @mirfan17
    The best way would be using WordPress REST API. You may follow this StackOverflow link and let me know how that goes! ??

    Moderator bcworkz

    (@bcworkz)

    It’s not clear where wp-load.php is relative to your file, but it’s likely your require path is incorrect. Relative path issues are part of why requiring wp-load.php is strongly discouraged. Such code is not portable to other installations. There are better ways. As Faisal recommended, the API is a very good way. For completeness, what was done prior to the API being available was to go through wp-admin/admin-post.php. It works much like WP Ajax where you send an “action” value with the request. In WP, you hook the action that’s in part named with the passed value with a callback that gets the data you want and sends it out to the requesting app.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Fetching loged In user data in external PHP file’ is closed to new replies.