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?

  • 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
    Commented Jul 12, 2011 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. Commented Jan 10, 2012 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
    Commented Sep 15, 2012 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
    Commented Sep 18, 2012 at 11:51
  • What is this external PHP app you're calling WP from?
    – Tom J Nowell
    Commented Sep 18, 2012 at 11:53

3 Answers 3


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.

  • thx! some tutorials say to include wp-blog-header.php .. that was the problem
    – yeahman
    Commented Apr 13, 2014 at 8:01
  • Live-saver. Thanks a lot, @shea! Commented Jul 13, 2020 at 20:46

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

  • 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. Commented Jul 12, 2011 at 17:40

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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