I have a WordPress multisite set up. Everything works fine except my ajax calls return 500s, this is something that I'm pretty sure started when I converted to multisite.
Snippet of Ajax Code that makes the call:
function ajaxShowComments(post,numberToLoad){
$.ajax({
url: eventComments.commenturl,
type: 'post',
data:{
'postId': post,
'comment-offset': offset,
'count': numberToLoad
},
success: function(result){
var arr = JSON.parse(result);
if( arr.length == 0 ) {
$('.readmore').css('display', 'none');
if( arr.length == 0 && offset == 0 )
$('.latestShows').append('<div id="nocomment"> No one has left a comment for this show yet! </div> <button class="first-comment-btn">Be The First!</button>')
$('.first-comment-btn').click(function () {
showModal();
});
}
arr.forEach(function (item) {
$('.readmore').before('<div class="lazy-comment">' + item + '</div>');
});
offset += numberToLoad;
}
});
}
Snippet of my themes functions.php, which creates the URL for the Ajax call:
function pebl_culture_assets() {
// Enqueue/register other styles/scripts etc...
wp_localize_script('pebl-main', 'eventComments', array(
'commenturl' => get_template_directory_uri().'/getcomments.php')
);
}
add_action( 'wp_enqueue_scripts', 'pebl_culture_assets' );
This works fine, script in html head looks like this:
//script tags omitted
var eventComments = {"commenturl":"http:\/\/peblarts.com\/{{SUBSITE NAME}}\/wp-content\/themes\/peblculture\/getcomments.php"};
So far so good, script loads the URL which is passed to the AJAX call (post, offset, numberToLoad all defined in js). The call is definitely made to getcomments.php which looks like this:
<?php
require_once("../../../wp-load.php");
$cmts = get_comments( array( 'post_id' => $_POST['postId'] , 'status' => 'approve') );
$comments = [];
$count = $_POST['count'];
if( !empty($cmts) ) {
for ($i = $_POST['comment-offset']; $i < $_POST['comment-offset'] + $count; $i++ ) {
if( !empty($cmts[$i]) )
array_push($comments, $cmts[$i]->comment_content);
}
echo json_encode($comments);
}else{
echo json_encode(array());
}
But it seems it breaks when trying to get the comments, very annoying, I have tried editing file permissions, tried changing the URL to explicitly point to where I know getcomments.php lives but that does not fix it. 500s all day long.