Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|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 '12 at 10:31
    
It sounds like a poorly designed architecture. The script is outside of WordPress, but depends on internal WordPress functions? Is the script on the same server as the WordPress installation? If so, why not just put it in a plugin? If not, it should include BackPress. What is it using the WP functions for? Maybe your theme should pass it all of the data it needs, so that it doesn't have to use the WP functions. –  Ian Dunn Sep 15 '12 at 5:15
    
This is a maintenance nightmare, using the WP AJAX hooks is much, much simpler and easier, far less effort –  Tom J Nowell Sep 18 '12 at 11:51
    
What is this external PHP app you're calling WP from? –  Tom J Nowell Sep 18 '12 at 11:53
add comment

3 Answers

You should be including wp-load.php, not wp-blog-header.php (which is reserved for use by WordPress only). This is most likely the cause of your problem.

share|improve this answer
    
thx! some tutorials say to include wp-blog-header.php .. that was the problem –  yeahman Apr 13 at 8:01
add comment

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?"

share|improve this answer
1  
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
add comment

Instead of including WordPress inside of 'Solution A', have you considered doing it the other way around, using the vastly superior/easier WP AJAX API and then including 'Solution A' inside WordPress?

Calling in wp-blog-header.php for AJAX is a massive mistake, I can't stress how big a headache you're introducing, security and maintenance wise ( e.g. this question demonstrates some of that )

What's the preferred method of writing AJAX-enabled plugins?

Also include wp-load.php not wp-blog-header.php

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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