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?

3 s
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.

Leave a Reply

Your email address will not be published. Required fields are marked *