duankuang7928 2016-09-09 17:52
浏览 30

Wordpress多站点Ajax错误

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘