I have a script that I'd like to call via AJAX from my WordPress theme, it relies upon some core WordPress functions so I am including WordPress blog header like so:

define( 'WP_USE_THEMES', false );
require( '../../../../wp-blog-header.php' );

Somewhere during the processing of wp-blog-header.php WordPress sets a 404 header which causes my AJAX request to fail, even though the script I am calling exists and is returning content.

I have since added status_header( 200 ); after that snippet which changes the status header back to 200 and solves the problem, however it feels very much like a hack, so I was just wondering if there is a recommended solution to this problem?

link|improve this question
1  
I think you get a 404 header internally, because WP_Query tries to resolve the requested page which it can't. I would look for some flag/const/trick to make WP_Query not execute / overload so to not needlessly burn resources for nothing. – hakre Jul 12 '11 at 16:35
I think this is a reasonable solution. I encountered the same problem in a different guise -- when sharing some code between a php application and a closely related WordPress site. PHP would generate a spurious 404 status code. Most browsers would ignore 404 status if content was returned; however, IE will sometimes fail to render the page if its "friendly HTTP error message" option is set. Setting status_header('200') after requiring wp-blog-header.php solved it. – Jan Hettich Jan 10 at 10:31
feedback

1 Answer

Yes there is a recommend solution for using WordPress Function via AJAX and that is using the wp_ajax hooks. you can see a very detailed example in my answer to "What's the preferred method of writing AJAX-enabled plugins?"

link|improve this answer
Thanks for your interesting reply, but that's not the problem I'm having. I'm calling a script external to the WordPress core that needs to include WordPress functionality, however during this process WordPress decides to set a 404 header. Sorry if my question wasn't very clear. – David Hancock Jul 12 '11 at 17:40
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.