PHP/AJAX only works when something gets echoed?

I’m trying to include the WordPress blog header in a php file to use it as an AJAX call function.

define('WP_USE_THEMES',false);

echo 'Something';

require(explode("wp-content",realpath(dirname(__FILE__)))[0].'wp-blog-header.php');

Original snippet found in: WordPress include(“../../../wp-blog-header”); failing, current by Ole Sauffaus.

Read More

The code only works when there is something echoed or printed between the define and the require function. Without it the server responds with a 404-error.

This behavior occurs only when I target the php via an AJAX request as follows.

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById('directory_results').innerHTML = xmlhttp.responseText;
    }
}

xmlhttp.open("POST", "http://localhost:8888/appsconnected/wp-content/themes/appsconnected/ajax-loop.php");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("platform=" + platforms + "&category=" + category + "&orderby=" + order);

What causes this behavior?

Related posts

1 comment

  1. Try this:

    <?php
    
        define('WP_USE_THEMES',false);
        require(explode("wp-content",realpath(dirname(__FILE__)))[0].'wp-blog-header.php');
    
    ?>
    

Comments are closed.