Json result from 'outside wordpress'
-
I need a stand alone backend php page (outside of wordpress) to return json results and also be able to utilize wpdb class for database access.
From what I read this should be fairly straight forward but 5 hours later I am still receiving some unexpected results. I have simplified the code down to the nuts and bolts but I’m not sure what is going on here.
The following code returns the expected json result:
{"id":"test"}
<?php define('WP_USE_THEMES', false); $jsonpost = array(); $jsonpost["id"] = "test"; $encoded=json_encode($jsonpost); die($encoded); ?>
However, when I attempt to include wp-load.php to bootstrap to the wordpress functions. I receive the following with the json result appended to the end:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml"> <head></head> <body></body> </html> {"id":"test"}
Here is the exact code that produced this result:
<?php define('WP_USE_THEMES', false); $jsonpost = array(); $jsonpost["id"] = "test"; $encoded=json_encode($jsonpost); //alter path to your specific location require_once("../../../wp-load.php"); die($encoded); ?>
The only difference was including the wp-load file. The link below is where I started however I have attempted other simple examples using cut and paste and it always returns the html page before json result when wp-load is included.
https://www.mlynn.org/2010/12/wordpress-extjs-displaying-posts-in-an-extjs-grid/
Any ideas on how to get this to work with wp-load file included?
- The topic ‘Json result from 'outside wordpress'’ is closed to new replies.