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:
<?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.
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:
Source: https://stackoverflow.com/a/4035357/835950