wp_get_current_user null if I directly access it in some PHP file

I am creating a plugin for WordPress. I created a simple PHP file:

Test.php. I can access that via browser but when I do:

Read More
<?php

global $wpdb;
global $wp;

?>

My wpdb and wp are NULL. Can anybody tell me what can be the issue? In short all wp functions are null. When does wordpress library actually set them? Normally your plugin will run in context of WordPress and you have the same menu of WordPress on left etc. But I need 1 file in my PHP which doesn’t run in context of WordPress but I need to access wordpress functions. It is a simple PHP file which should return just XML no other HTMl etc.

Related posts

Leave a Reply

1 comment

  1. The issue is that those variables are not set. So they are NULL.

    If you set them first, they won’t be NULL.

    Try including the wp-load.php file prior making use of these global variables and see if that fits you’re needs.

    See as well:

    Code:

    //This path assumes your php file is in the template dir, you may need to update the path to these files.
    
    include_once('../../../wp-load.php');
    

    Source: https://stackoverflow.com/a/4035357/835950