I have an application that is built around Wordpress. It is fairly bespoke but none-the-less I still leverage and appreciate a lot of what comes with Wordpress too. Now what I'm trying to do is provide a RESTful API that will work off of the Wordpress database. I'm using an open source product called Restler that I'm very happy with so far which makes providing this service very easy with PHP but I'd like to have this PHP load the Wordpress machine so that I can leverage my already built code more easily as well as the handy Wordpress functions.
With Restler you have a PHP class file that defines the interface; I added the import at the beginning to load the WP environment:
<?php
include "AddWordpress.php"; // loads wordpress in AJAX mode
/**
* All actions that the system will process through this service.
*
* @package lg-api
* @return json LG_JSON_Activity
*/
class Actions {
/**
* LIST action types
*
* List all the action-types available
*
* @url GET /
*/
function list_actions() {
$actions = new LG_TAX_Action();
$action_list = $actions->dataset();
return "\n" . json_encode($action_list) . "\n\n";
}
}
where AddWordpress.php is (debugging set on for dev env):
<?php
error_reporting(E_ALL);
$site_name='yoursite';
$site_domain='www.yoursite.com';
/**
* Construct a fake $_SERVER global to get WordPress to load a specific site.
* This avoids alot of messing about with switch_to_blog() and all its pitfalls.
*/
$_SERVER=array(
'HTTP_HOST'=>$site_domain,
'REQUEST_METHOD'=>'GET',
'REQUEST_URI'=>"/{$site_name}/",
'SERVER_NAME'=>$site_domain,
);
// Remove all our bespoke variables as they'll be in scope as globals and could affect WordPress
unset($site_name,$site_domain);
// Pretend that we're executing an AJAX process. This should help WordPress not load all of the things.
define('DOING_AJAX',true);
// Stop WordPress doing any of its normal output handling.
define('WP_USE_THEMES',false);
// turn on debugging
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
// Load WordPress - intentionally using an absolute URL due to issues with relative paths on the CLI.
include "/[path-to-root-www]/wp-load.php";
This approach is great for creating command-line test files before jumping into the real code and testing through a browser but for some reason when I run it with Restler I get a slew of errors such as this:
Notice: is_404 was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 294
Notice: is_home was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice: is_search was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice: is_archive was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice: is_404 was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice: is_tag was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice: is_search was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice: is_category was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
If I were to simply add the following code to the bottom of the class file and then run it from the command line it works without a problem:
$foo = new Actions();
echo $foo->list_actions();
Can anyone give any pointers on why this might be occurring?
UPDATE
I've changed the error handling to give me a stack trace and get the following:
#0 /[path-to-www-root]/wp-includes/query.php(708): _doing_it_wrong('is_404', 'Conditional que...', '3.1')
#1 /[path-to-www-root]/wp-content/themes/pagelines/includes/library.functions.php(68): is_404()
#2 /[path-to-www-root]/wp-content/themes/pagelines/includes/library.options.php(43): is_pagelines_special(Array)
#3 /[path-to-www-root]/wp-content in /[path-to-www-root]/wp-includes/functions.php on line 2944
So the initial call seems from my theme making calls into query.php. It's more information but I'm not sure how much insight it really provides as I'm pretty sure the way to solve this is to tackle why initial state of the import varies between running from the command line versus trying to trace back all the problems occuring from the poor state it started in.