I am currently working with a straightforward script as shown below:
$(function() {
var url = theme_directory + '/func/api.php';
$.get( url, function(data) {
alert("Data Loaded: " + data);
});
});
Here is the code for api.php:
<?php
echo('hello');
define('WP_USE_THEMES', false);
require_once( dirname( __FILE__ ) . '/../../../../wp-blog-header.php' );
Initially, this setup functions properly. However, when I rearrange the code like this:
<?php
define('WP_USE_THEMES', false);
require_once( dirname( __FILE__ ) . '/../../../../wp-blog-header.php' );
echo('hello');
And access the api.php file directly, 'hello' is displayed. Yet, the JavaScript alert does not show up. When I move 'hello' back to its original position, the alert works again. This inconsistency is puzzling.
<?php
echo ' ';
define('WP_USE_THEMES', false);
require_once( dirname( __FILE__ ) . '/../../../../wp-blog-header.php' );
print_r($wp);
In this scenario, the JavaScript successfully displays the contents of $wp variable. However, upon removing echo ' ';
, the JavaScript alert fails to trigger.
This issue seems to be connected to WordPress Permalinks. It functions correctly when Permalinks are disabled, but it malfunctions when they are enabled.